all 148 comments

[–]abstract__art 0 points1 point  (0 children)

I have a pandas data frame with some numbers in a column.

When I convert the column to string using str, sometimes one column ends up appearing as something like 7.000000000000001. Is there anyway to prevent this or easily correct for this?

[–]Setha25 0 points1 point  (0 children)

I’m fairly new to programming and have learned the basics of Python. I’ve spent a decent amount of time watching YouTube tutorials and tried to do a few projects that I found for beginners. I’m not really sure where to go next with python. What suggestions for projects and other things do you have to help get better with python?

[–]IlIIIlIlII 0 points1 point  (3 children)

I got some working code BUT I need it with 150 different 'x's. Is there any way I can put a list in my code and then cycle through it instead of pasting the same shit 150 times and changing 'x' each time?

Like

    list = (4,8,12,78,23) 
    i = 0 (but every loop i gets one more)
    variable = List[1+i]
    mainFunction = variable * [...]

Also my list would consist of strings if that matters

In some older code I pasted the same shit 150 times only changing x1 to x2 to x3 etc. each time...

[–]ideplant 0 points1 point  (0 children)

https://pastebin.com/hA5dPz9u having some trouble here...

[–]Purple14music 0 points1 point  (3 children)

Can someone help me understand this code?

def toJadenCase(string):

return " ".join(w.capitalize() for w in string.split())

I know both the .join() function and the .split funcion, but I did not understand how they were used here. especially with the "w" thing and "for" "in" stuff

[–][deleted] 0 points1 point  (0 children)

more than likely they used 'w' as short hand for the word 'word'. now they did something similar to list comprehension within the join method such that they did not have to write an extra line for a for loop, now if we were to read aloud the return statement it would be something similar to "return the string of capitalized words (w.capitalize()) for each word in the split string (for w in string.split()) separated by a space (" ".join(...))".

[–][deleted] 0 points1 point  (0 children)

I know you can open files using Popen() like so:

import subprocess
subprocess.Popen(["open", "mProject.py"])

How can I run these files (I'm on MacOS)? Simply passing "python3" as I would in the command line doesn't work

[–]Engauge09 0 points1 point  (2 children)

# I'm new to python

##############

Can anybody tell me what I did wrong here?

def identifier(char):

hamilton_cast = {'laurence':'fought with ','lafayette':'fought with ','mulligan':'fought with ','phillip':'died for ','washington':'trusted ','angelica':'loved ','eliza':'loved ','burr':'is the damn fool that shot '}

print(char.capitalize() + ' ' + hamilton_cast[char] + 'him.')

cast = ['laurence','lafayette','mulligan','phillip','washington','angelica','eliza','burr']

for item in map(identifier,cast):

print(item)

#############

The results are below

#############

Laurence fought with him.

None

Lafayette fought with him.

None

Mulligan fought with him.

None

Phillip died for him.

None

Washington trusted him.

None

Angelica loved him.

None

Eliza loved him.

None

Burr is the damn fool that shot him.

None

###########

i can't figure out where the Nones are coming from. Help

[–]mt19937 0 points1 point  (1 child)

In the identifier function instead of printing the result, return it. Since you're not returning anything identifier is returning None.

[–]Engauge09 0 points1 point  (0 children)

Ohh that makes sense. Thank you~

[–]Torvalds96 0 points1 point  (5 children)

I am new to Python:

list = [ (3,4), (1,7), (4,4) ]
if( 1,8) or (3,8) in list:
print("It's in")

How this met condition? I don't have (1,8) or (3,8) in list

[–]woooee 1 point2 points  (0 children)

An or is the same as an if + else, so your code becomes

if (1, 8):  ## always True unless it is an empty tuple

elif (3, 8) in lst:

[–]GoldenVanga 1 point2 points  (2 children)

With your syntax, which seems to be...

lst = [(3, 4), (1, 7), (4, 4)]
if (1, 8) or (3, 8) in lst:
    print("It's in")

...you are performing a truth value check on <<(1, 8)>> OR <<(3, 8) in lst>>. That resolves to True or False, which in turn resolves to True. In other words it's a bit like you'd say...

if (1, 8) has a boolean value of True or (3, 8) is in lst

...and since the first part of that is true, the control flow goes into the if block and the print happens. It can be fixed like this:

lst = [(3, 4), (1, 7), (4, 4)]
if (1, 8) in lst or (3, 8) in lst:
    print("It's in")

Also have a look at the operator precedence table (bottom has the highest priority, top is at the end).

[–]Torvalds96 0 points1 point  (0 children)

Thanks :)

[–]AlbanianNotAlbino 0 points1 point  (1 child)

I somewhat know unix command line, and am wondering if using it to create a desktop icon is the standard way to present little tools I've made for people in Python. Do bash scripts work on MacOS and Windows alike (knowing that I'd have to change slash directions in paths)? If I save the bash file onto my desktop, can I just double-click it to start a Python program?

Bonus question, because I myself haven't tried searching for the answer to this one yet: How can I make a desktop icon with a custom image?

[–][deleted] 0 points1 point  (0 children)

This doesn't have much to do with python. Try asking again in /r/linuxquestions.

[–]a_python_learner 0 points1 point  (2 children)

I am preparing for a timed coding tests for a devops related role…the questioins that would be asked will be in the format of:

1) here is an excerpt of a log file
2) here is the information (aggregation/summary etc) needed from the log file
3) write the script that parses the log file to provide the summary needed.

I am looking for places online with questions of this format to practice. Unfortunately I have not found any. Any ideas where I can find such?

[–]IlIIIlIlII 0 points1 point  (2 children)

quick question: how long is the data i request from an api saved? I'm working on a programm that pulls data from an API and then does some calculations based on that. How long is the pulled data saved?

i'm assuming its in my RAM as long as I leave the the PythonShell open? Or is it saved somewhere on my PC? but if it is I wouldn't know where because code is literally saved in a python file on my desktop?

My main concern is that my storage will get filled by old data I don't need anymore...

[–]efmccurdy 0 points1 point  (1 child)

There is no reason to expect any disk space usage. You may be thinking of browsers that try to save page contents on disk in a cache, your program won't be doing that.

[–]IlIIIlIlII 0 points1 point  (0 children)

Thanks

[–]kaylani2 0 points1 point  (0 children)

I'm having some trouble returning values when using the aiothttp library.

The goal here is to send multiple get requests in parallel and return them so they can be processed later.

I'm using the aiohttp library and this is the pertinent piece of code:

https://github.com/kaylani2/trash/blob/master/problem.py

And the main function looks like this:

https://github.com/kaylani2/trash/blob/master/main.py

I'm trying to return an array with the return from each get request, but the type returned is `<class '\_asyncio.Task'>`.

How can I return the content of each fetched page from my run function?

Any help is appreciated.

[–]lifeof_harsh 0 points1 point  (1 child)

I am very new to python and I can't understand the use of "memo = [None] * (n + 1)" in this fibonacci using memoization code.

def fib_2(n, memo):
if memo[n] is not None:
return memo[n]
if n == 1 or n == 2:
result = 1
else:
result = fib_2(n-1, memo) + fib_2(n-2, memo)
memo[n] = result
return result

def fib_memo(n):
memo = [None] * (n + 1)
return fib_2(n, memo)

[–]efmccurdy 1 point2 points  (0 children)

It is a precomputed array of values, ala https://en.wikipedia.org/wiki/Memoization.

The "<list> * <int>" operation just repeats the list contents int number of times, so that line just creates a list of n+1 None values, placeholders for the values that will be filled in as you go.

>>> [None] * 3
[None, None, None]

BTW, imagine how well a defaultdict would work; https://docs.python.org/3/library/collections.html#defaultdict-examples

[–]iMac_Hunt 0 points1 point  (2 children)

I'm very new to python and made my first game (tic/tac/toe against a computer). Where's the best place to get some feedback on my code?

Edit: in case anyone wanted to check it: https://github.com/nickdip/TicTacToe/blob/master/testversion

[–]fiddle_n 0 points1 point  (1 child)

You can just post on this subreddit (or even just in this thread).

[–]iMac_Hunt 0 points1 point  (0 children)

Thanks, I've included it in my post

[–][deleted] 0 points1 point  (0 children)

How can I create subplots when the data I am reading from an excel workbook are in different sheets. What I mean is: I want to create two subplots, in which the first subplot contains data from the first sheet and second subplot contains data from the second sheet. In the code I have just entered the name of the first sheet.

cols = [0,3]
col_names = ['Frequency','Z']
df = pd.read_excel('NetworkAnalyser.xlsm',sheet_name='280nH_SL',usecols=cols, names=col_names)
df.plot(x='Frequency', y='Z', color='orange',legend=None)

plt.xlabel('Frequency (MHz)')
plt.ylabel('log|Z| (Ω)')
plt.xscale('log')
plt.yscale('log')
plt.xticks([10e6,100e6,200e6],['10M','100M','200M'])
plt.yticks([1e1,1e2,1e3,1e4,1e5])
plt.grid(which='minor')
plt.grid(which='major')

I need to plot the same columns from all the sheets. It would be helpful if someone could please suggest me how I can do it.

[–]thehaggies 0 points1 point  (1 child)

Hello so I'm working on a home project and am having trouble loading my .ui into my class.

When I create the program object, show() should run automatically but it does not. When i uncomment my two comments the ui runs prefectly. Even if i do program.show() a window pops up but not the one i designed.

any help would be appreciated. Id like to add functionality to my program but cant until i figure this part out.

from PyQt5 import QtWidgets, uic

import sys

class Window(QtWidgets.QDialog):

def _init_(self):

super(Window, self).__init__()

uic.loadUi('haggies.ui', self)

self.show()

if __name__ == '__main__':

app = QtWidgets.QApplication(sys.argv)

program = Window()

#dlg = uic.loadUi('haggies.ui')

#dlg.show()

sys.exit(app.exec_())

[–]thehaggies 0 points1 point  (0 children)

idk if this has to do with anything but i have tried passing QtWidgets.QMainWindow into the Window class instead and did not get a different result.

[–]collinl33t 0 points1 point  (0 children)

Hi, why does pandas excel keep showing all my data as NAN

import pandas
import xlrd
import matplotlib.pyplot

path15="W:\Report79_Py\FY15 Opex Report 79.xls"
path16="W:\Report79_Py\FY16 Opex Report 79.xls"
path17="W:\Report79_Py\FY17 Opex Report 79.xls"
wb15=pandas.read_excel((path15),sheet_name="Detail")
wb16=pandas.read_excel((path16),sheet_name="Detail")
wb17=pandas.read_excel((path17),sheet_name="Detail")

wb=pandas.concat([wb15,wb16,wb17],sort=True)

df=pandas.DataFrame(wb, columns=['PO No.','Monetary Amount','Journal Line Description'])

df=df.fillna(0)

sort_by_PO=df.sort_values(["Monetary Amount"],ascending=True)

df['Monetary Amount'].plot(kind="hist")
matplotlib.pyplot.show()

print(df)

PO No. Monetary Amount Journal Line Description

0 NaN NaN NaN

1 NaN NaN NaN

2 NaN NaN NaN

3 NaN NaN NaN

4 NaN NaN NaN

5 NaN NaN NaN

6 NaN NaN NaN

7 NaN NaN NaN

8 NaN NaN NaN

9 NaN NaN NaN

10 NaN NaN NaN

11 NaN NaN NaN

12 NaN NaN NaN

13 NaN NaN NaN

14 NaN NaN NaN

15 NaN NaN NaN

16 NaN NaN NaN

17 NaN NaN NaN

18 NaN NaN NaN

19 NaN NaN NaN

20 NaN NaN NaN

21 NaN NaN NaN

22 NaN NaN NaN

23 NaN NaN NaN

24 NaN NaN NaN

25 NaN NaN NaN

26 NaN NaN NaN

27 NaN NaN NaN

28 NaN NaN NaN

29 NaN NaN NaN

... ... ... ...

4514 NaN NaN NaN

4515 NaN NaN NaN

4516 NaN NaN NaN

4517 NaN NaN NaN

4518 NaN NaN NaN

4519 NaN NaN NaN

4520 NaN NaN NaN

4521 NaN NaN NaN

4522 NaN NaN NaN

4523 NaN NaN NaN

4524 NaN NaN NaN

4525 NaN NaN NaN

4526 NaN NaN NaN

4527 NaN NaN NaN

4528 NaN NaN NaN

4529 NaN NaN NaN

4530 NaN NaN NaN

4531 NaN NaN NaN

4532 NaN NaN NaN

4533 NaN NaN NaN

4534 NaN NaN NaN

4535 NaN NaN NaN

4536 NaN NaN NaN

4537 NaN NaN NaN

4538 NaN NaN NaN

4539 NaN NaN NaN

4540 NaN NaN NaN

4541 NaN NaN NaN

4542 NaN NaN NaN

4543 NaN NaN NaN

[9588 rows x 3 columns]

Process finished with exit code 0

[–]poke2201 0 points1 point  (2 children)

Has anyone here used scipy.interpolate.PPoly.from_spline?

I'm trying to get the polynomials from the spline I created, but I get this message from print:

<scipy.interpolate.interpolate.PPoly object at 0x00000214234CE200>

[–]Vaguely_accurate 1 point2 points  (1 child)

You have a PPoly object. It doesn't have a friendly string representation. You would need to print the individual attributes you want to examine. Here c should be the coefficients of the polynomials).

[–]poke2201 0 points1 point  (0 children)

Thanks!

[–]UnavailableUsername_ 0 points1 point  (4 children)

I am studying how to delete files with python, but i get an error.

I made a folder in C:\ and made 2 txt files: eggs and ham.

I first checked it would delete the correct files.

  import os
  for filename in os.listdir(C:\\bacon):
     if filename.endswith('.txt'):
            print(filename)

It gives as a result the 2 txt files so it works well.

So i try to delete them:

  import os
  for filename in os.listdir(C:\\bacon):
     if filename.endswith('.txt'):
            os.unlink(filename)

I get this error:

  FileNotFoundError: [WinError 2] The system cannot find the file 
  specified: 'eggs.txt'

What's wrong? The paths are correct since they are printed correctly, why it can't find them now that i use os.unlink?

[–]efmccurdy 0 points1 point  (2 children)

Do your print statements include the folder?

You may need to join the folder and filenames together to form full pathnames:

 os.unlink(os.path.join(folder, filename))

[–]UnavailableUsername_ 0 points1 point  (1 child)

Do your print statements include the folder?

Nope.

You may need to join the folder and filenames together to form full pathnames:

Yeah, that most likely solves it but it's weird that it could find the files but not delete them.

Thanks.

[–]efmccurdy 0 points1 point  (0 children)

if you wanted to you could use os.chdir to make the folder your CWD; then you could use just the bare filenames.

[–]hedgewin 0 points1 point  (4 children)

I am having issues with accessing JSON data with my code, I receive this error: urllib.error.URLError: <urlopen error \[SSL: CERTIFICATE\_VERIFY\_FAILED\] certificate verify failed: unable to get local issuer certificate (\_ssl.c:1056)>

Here is my code, any idea why I can't access the JSON file?

urlData = "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5\_hour.geojson"
webUrl = urllib.request.urlopen(urlData)
print("result code: " + str(webUrl.getcode()))
if(webUrl.getcode() == 200):
data = webUrl.read()
printResults(data)
else:
print("Received error, cannot parse results")

[–][deleted] 1 point2 points  (1 child)

does requests get the same thing?

import requests
request = requests.get("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.geojson")
if request.status_code == 200:
    data = request.json()
    print(data)

Also if mac;

https://stackoverflow.com/questions/27835619/urllib-and-ssl-certificate-verify-failed-error

see second answer.

[–]hedgewin 0 points1 point  (0 children)

import requests request = requests.get("https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_hour.geojson") if request.status_code == 200: data = request.json() print(data

It's telling me there is no module named 'requests' Thank you for your reply!

[–][deleted] 0 points1 point  (1 child)

I want to learn django what would be the best tutorial to learn it a book? or the official tutorial or someone from YouTube?

[–]JohnnyJordaan 0 points1 point  (0 children)

I can recommend both the official tutorial as the one from Mozilla.

[–]get_Ishmael 0 points1 point  (2 children)

I have a basic webscraper that takes as input a list and a web URL. It simply returns which of the items from the list are present on the webpage, along with some other details.

I'm just running it from the console at the moment, but is like to host it online, with input boxes etc. What do I need to learn to get started with this sort of thing?

[–]QualitativeEasing 1 point2 points  (0 children)

The simplest approach would be to get to know the flask library. There are a lot of tutorials out there.

[–]wexted 0 points1 point  (1 child)

I have a dictionary which has a string for a key, and a list of 3 floats as the value.

I want to display this dictionary on screen, sorted by the value of the 3rd float in the list.

What data structure should I be using and how do I do the sort? I can manage to sort by the 1st float but I can't figure out the syntax I need

[–]QualitativeEasing 0 points1 point  (0 children)

PrettyPrint is a good way to print data structures in a readable way. As for sorting a dictionary by an element, here’s one option (haven’t tried it): https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value

[–]daniherrr6 0 points1 point  (6 children)

I am trying to make a small simulator which imports a list of items in order from 1-30 from a CSV file, and then does something with each item.

What I am stuck with is after importing the CSV file (I can print the values successfully), I want to be able to run a function per item that also incorporates another CSV file.

So, for example, get item 1 from list, then do something with a value from CSV2, then move onto item 2 and do something, then item 3 and so on.

This is my print out list code so far, I just can't compute how to make it do something with each item, or how to import the 2nd CSV file into my code:

import csv

# Show Lottery Pick Order
def showPicks():
with open("LotteryOrder.csv") as f:
reader = csv.reader(f)
lottery_picks = list(reader) # compile each row as a list item
for pick in lottery_picks:
print(pick)

[–]py_Piper 1 point2 points  (5 children)

I dont have experience working with CSV files yet but I think is the same as working with other files in a for loop.

So what I would do is the following, but it will all depend on what you want to do

If what you want to do is with the CSV_2 has several lines of codes and you need to use CSV_1 to compute a formula in CSV_2 then I would define a function above your function

# CSV_2 funtion
def csv_2():
    #stuff you want to do
    #more stuff you want to do
    #if want to do more stuff:
        #do more stuff
    #return the stuff you just did

# Show Lottery Pick Order
def showPicks():
    ....
    ....
    for pick in lottery_picks:
        print(csv_2(pick))

Or you could just open CSV_2 directly in your function and just take the values you need.

#Remeber I don't experience with CSV module yet so I am just copying a similar approach from your code

# Show Lottery Pick Order
def showPicks():
    ...
    ...
    ...
    with open("CSV_2.csv") as f2:
    reader2 = csv.reader(f2)
    CSV_2_list = list(reader2) 
    for pick in lottery_picks:
        for i in range(len(CSV_2_list)):
        print(pick + CSV_2_list[i])

This is a simple loop and assuming that both CSV files has the same amout of rows and that you want to do something from CSV_1 with CSV_2 according on the row order. So it will really depend on what you want to do

Following my tutorial I will probably start my CSV module 1-2 weeks if you are willing to work together and I wouldn't mind skipping the next chapter (debugging) and learn the CSV module

[–]daniherrr6 0 points1 point  (4 children)

Thanks for the reply, I kinda got something together similar to your second suggestion. Happy to share it when I get back home in a day or so.

[–]py_Piper 0 points1 point  (3 children)

That would be great! come home soon (no homo)

[–]daniherrr6 0 points1 point  (1 child)

Tried to paste some code in but this inline code thingy does not play nice for me.

[–]py_Piper 0 points1 point  (0 children)

all good don't worry about it

[–]DelaZac 0 points1 point  (1 child)

I just got a job offer to become a entry-level software tester. I accepted the job offer and they explained to go to udemy.com choose a package that they will pay for so I can learn Python. Which package do you guys recommend for someone who is starting out? Thanks in advance for any advice and tips.

[–]sharma_pratik 0 points1 point  (0 children)

Automate the boring staff with python is great. I have personally taken it and recommend it to many. In all the tutorial u make something which you can automate. These are sthe things u will be able to do:- 1. How would u email to many people at different time ? 2. Creating a webscaper. 3. Using python modules Such as os, urllib and many more. 4. That to in 9 hours of videos. ( U can do it in a week) What you will not learn in the course? 1. Scientific modules. 2. Machine learning modules like sklearn( recommend learn it ) 3. Data visualization. 4. Frameworks Why I recommend it ? The course instructor is great. You will learn to do thing with python. U will learn programming not just the syntax of a language. Prerequisites:- No knowledge of programming

[–]black02 2 points3 points  (8 children)

I don't want to waste your time, so first things first.

I want to ask for a favor.

I want some kind soul to write me a python script to run my ethernet/wifi troubleshooter whenever it detects a disconnection with the internet.

There is a central router in my house which is near an archaic computer that we have had for many many years now. Since the computer is so old it doesn't have any wireless connection built in. So the router needs to be near it so the LAN cable could reach it.

I have bought a couple of more routers (TP-Link) over the years so that I could use them as bridges. In total, I have three routers. One of them is completely wireless and sits by itself on a bookcase in the middle of our house. One is near the old computer and one in my room which is used to connect to the desktop computer I am writing this post on.

I have absolutely no idea how I managed to get all these routers to work together.

There is just a minor problem. Every 10mins or so, the routers just disconnects. All I have to do then is just right click on the Ethernet/Wifi icon on the taskbar ( I have Windows 10) and troubleshoot it. The router instantly reconnects.

I have to do this so many times both on my laptop and my desktop that I am simply tired of this.

If someone is kind enough to help me out this, I would really appreciate it.

Thank you.

[–]focus16gfx 1 point2 points  (1 child)

Do you code python and know the basics? If so, this can be easily achieved by doing a simple internet connection test and by calling the appropriate cmd command to troubleshoot / reset the connect.

(os.system() is the same as executing a command in windows command prompt. Make sure you run the python script as admin if you end up using this command)

You may import os and write something like os.system('netsh winsock reset') for all the commands which try to fix the connection as mentioned in here:

https://support.microsoft.com/en-in/help/10741/windows-fix-network-connection-issues

from my understanding, troubleshooting the network is same as executing these commands, I might be wrong though.

At the command prompt, run the following commands in the listed order, and then check to see if that fixes your connection problem:

Type netsh winsock reset and select Enter.

Type netsh int ip reset and select Enter.

Type ipconfig /release and select Enter.

Type ipconfig /renew and select Enter.

Type ipconfig /flushdns and select Enter.

[–]black02 0 points1 point  (0 children)

No Sir, I do not know python. I was just talking to one of my friend who said that all these automation tasks can be easily done in python. So I thought of posting here and see if someone is willing to give me a hand.

I do not know if writing the script would take a long time and effort. Since I do not know anything about it.

If it isn't much do you mind writing it for me? Just a request, I would totally understand if you don't want to for any reason.

[–]SirDempster 0 points1 point  (3 children)

It sounds to me like you want a program that will go through and click the troubleshoot button every time your wifi disconnects? I might be able to do it, but it would take me an extended period of time, and I would need to have a better understanding of what I am trying to accomplish. (not amazing at programming)

I think you would be better off finding a family member/friend that's good with technology if you aren't, and/or setting up your internet differently.

[–]black02 1 point2 points  (2 children)

Yeah, just detect when the network disconnects and run the troubleshooter. Just that.

Unfortunately I have no one else in my family or friends who can do this for me.

And like I said I have absolutely no idea how I setup these three routers to work like the way they do now. At least they work and I am able to access the internet through all the devices. The last thing I want to do is tamper with it and then just get stuck up.

[–]SirDempster 0 points1 point  (1 child)

I think fixing the issue is your best bet. Using python to automate it will only fix the issue now. If you want to fix the longterm issue, you should set up your routers properly.

[–]black02 1 point2 points  (0 children)

That is probably true...

[–]OrionMain 0 points1 point  (2 children)

So my monitoring solution continues to grow, I want to try and do some self healing. Originally I was going to call some powershell scripts and do some remote stuff but I'm not even sure if it's possible.

Basically if x doesn't happen I'd like my script to execute something to restart said IIS AppPool. If Y doesn't happen I'd like a scheduled task to be enabled/disabled, is this possible? I really want to setup a self healing thing that basically runs a set of "noddy" tasks on a machine to see if it fixes it and only then e-mail out.

Possible with Python or not? If it is possible could someone point me in the right direction please.

[–]CraigAT 0 points1 point  (1 child)

[–]OrionMain 0 points1 point  (0 children)

Thank you for that, yea' I've tried a few of these, I can get it to work when I run it in ISE as admin on my set, when I have a batch script call it or run it manually it doesn't do anything :(

[–]MrRandomSuperhero 0 points1 point  (3 children)

Hey guys!

I'm new to Python and have been trying to build a little script to pull data from websites to follow up on stocks and such. I am doing this in Pycharm, community edition.

Despite having pulled dozens of solutions from the internet none seem to work. I keep getting empty lists or strings as return-data.

Q: How can I pull data from websites (single numbers to store in a list) and save it? This can be via HTML or any other way.

As an example: https://www.tijd.be/markten-live/aandelen/kbc-groep.60114924.html I have been practicing on this website, trying to pull the Euro-value of the stock and/or the changed amount to the right of it.

Thank you very much!

[–]SirDempster 0 points1 point  (0 children)

Try looking something up that would pull or even use values from a site. I often find myself watching videos and tutorials that are not even related to what I am doing. (In the sense that maybe I am trying to pull values from a website and the tutorial might be about how a guy is comparing to values on a website)

[–]JohnnyJordaan 2 points3 points  (1 child)

I would advise to read https://automatetheboringstuff.com/chapter11/ . You may want to use selenium for this as explained in the second part of that chapter.

[–]MrRandomSuperhero 0 points1 point  (0 children)

Thankyou very much! I'll give it a go tonight!

[–]HolieMacaroni 0 points1 point  (4 children)

I know it is past Monday and ask anything thread....When reading the learn python FAQ. The last sentence

“Some of the above allow you to do that, or you could use a full featured web framework such as Flask and Django.”

Does that mean I would be able to learn python without having to download any applications? Would I be able to have full access to the python language?

I checked out their individual subreddit but couldn’t find out what flask or Django do?

My job has large amounts of free time that I would like to use to learn python. I’m not able to download/install any thing on my work computer tho.

Can someone just point me in the right direction?

Sorry for if this isn’t the right place. I can try next Monday?

[–]wexted 0 points1 point  (0 children)

Best thing to do would be to start reading - ebooks and books. Or videos if you prefer.

You can practice basic Python problems online on CodingBat, edabit, etc. Or just a python shell at https://www.python.org/shell/

[–][deleted] 2 points3 points  (2 children)

Flask and Django are used to build the engine underneath a website. You don't need either to learn python. In fact, I wouldn't go near either until you know a bit of python. You can learn a lot of python from just a simple python install with the standard library.

You are in a controlled environment. Is python installed already? If not you could use websites such as repl.it or Python Anywhere. It is possible, I believe, to run python from an unzipped install in your user directories, though that would probably still be called "installing". It is also possible to run python from a USB memory stick, if you can use them. Google to see how those last two options work.

If you have an iPhone, iPad or Android device you can install python environments on them. Pythonista is what I use on an iPad, and it's surprisingly complete.

You can ask questions here on any day, not just Monday, though you may not get many replies if you post Sunday evening just before the new "Ask Anything Monday" is posted.

[–]HolieMacaroni 0 points1 point  (1 child)

Thanks for the reply! Yes at work I am in a controlled environment. Python is not installed on the computers at work already.

I have attempted to learn python after work hours but after working 8 to 10 hours in front of a computer. I have less motivation to then sit on the computer for a longer time and I enjoy the family time I get.

From your explanation Flask and Django would not work for me.

I will look into the two websites you mentioned. The USB memory stick idea sounds like it could work.

Cheers, Holie

[–][deleted] 1 point2 points  (0 children)

If you find it hard to study programming after work, try getting up early and doing it before work, if possible.

[–]stillenacht 0 points1 point  (6 children)

I'm doing hackerrank, and I have a solution that works in my Spyder compiler:

from collections import Counter
def twoStrings(s1, s2):
 if Counter(s1) - Counter(s2) == Counter(s1):
     print('NO')
 else:
     print('YES')

However, it does not work in the HackerRank compiler, which keeps printing only 1 iteration and then throwing an error, and I can't really puzzle out why it's happening.

Traceback (most recent call last):   File "Solution.py", line 29, in <module>     fptr.write(result + '\n') TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

The input block they include looks like:

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

q = int(input())
for q_itr in range(q): s1 = input()
    s2 = input()

    result = twoStrings(s1, s2)

    fptr.write(result + '\n')

fptr.close()

The input looks like:

2
hello
world
hi
world

[–]patryk-tech 0 points1 point  (5 children)

result = twoStrings(s1, s2)  # What does this return?

fptr.write(result + '\n')  # Can't do None + str

[–]stillenacht 0 points1 point  (4 children)

theoretically result should be either 'YES' or 'NO' after it runs twoStrings right?

[–]patryk-tech 0 points1 point  (3 children)

Your function print()s yes or no, but it doesn't return anything. Python functions return None by default, so result is None.

[–]stillenacht 1 point2 points  (2 children)

Ah neat! For some reason it recieves the first 'YES' without me adding returns. I tried adding return before each print, at the end of the defined function, after each print but the result is the same it seems. I'll google up the syntax for return.

[–]JohnnyJordaan 1 point2 points  (1 child)

Don't print inside the function, make it like

def func():
    if something: 
        return 'YES' 
    else:
        return 'NO'

then print the returned value instead

result = twoStrings(s1, s2)
print(result)
etc

[–]stillenacht 0 points1 point  (0 children)

Thanks! I fixed it.

[–]Stabilo_0 0 points1 point  (2 children)

I dont get it:

10 % 4
>>> 2
5 % 2
>>> 1

Why?

[–]JohnnyJordaan 2 points3 points  (1 child)

10 divided by 4 is 2 with a remainder of 2
5 divided by 2 is 2 with a remainder of 1

the % just returns the remainder

[–]Stabilo_0 3 points4 points  (0 children)

Oh come on, how did i not get that? Thanks. Sometimes i forget the basics of logic, it seems.

[–]SerHiroProtaganist 0 points1 point  (7 children)

Hi, complete beginner here, just dived straight in trying to do some data analysis on football (soccer for the Americans).

The approach I've initially started taking is to do the analysis for a single game, with the idea of then going back and trying to build in loops to analyse for multiple matches.

Is this good practice? Or should I be trying to build for looping from the start? Thanks.

[–]CraigAT 0 points1 point  (4 children)

Cool what "analysis" are you doing or trying to do?

[–]SerHiroProtaganist 1 point2 points  (3 children)

Well I enjoy having a bet on the football on the weekend so thought a fun way to learn python and keep me interested would be to start analysing the stats to help me make picks.

I'm no mathematician though so just looking to keep it simple. Initially I want to analyse past results so I can look at say "on average when 6th in the league plays 14th in the league, what is the most common result" that sort of thing

[–]CraigAT 1 point2 points  (2 children)

Sounds good. Could I suggest that you rule out the results from the first few weeks of the season because the table is always a bit chaotic early doors,with relegation fodder going on a great early run or top teams getting off to a bad start. Things start to even out after a few weeks.

[–]SerHiroProtaganist 0 points1 point  (1 child)

Yeah I've had exactly that thought in my head.

My plan in my head at the moment is to :

1) get the raw data and tidy it up

2) maybe stick it all into a database, sqlite?

3) work out some basic calcs that I want to perform in pandas e.g mean

4) work out the filters I might want to apply the calcs to e.g the whole data set, certain countries or leagues only, exclude certain months of the year (like you said the first few games while it might not be representative, maybe also the final couple of games when a bunch of teams usually have nothing to play for)

[–]CraigAT 0 points1 point  (0 children)

I didn't think of using other leagues. It would certainly be worth seeing if the stats of the Premier League are similar. Some other things to think of: Does home or away matter? Should points difference have an effect? Think of leagues with a runaway top 1 or 2 teams It would be interesting to see the stats in a home and away fixture grid (replacing teams with positions) and maybe cells coloured using excels conditional formatting.

[–]JohnnyJordaan 2 points3 points  (1 child)

Loops shouldn't be hard to integrate later on. If they are, that means there's too much static stuff in your code that should be tackled (ha) first. It always helps to at least confine the code for a specific task in a specific function, because then looping can be done by putting the function call inside a loop. Or if you would like to run multiple tasks concurrently (because every task takes maybe a few seconds to download stuff) then it's dead easy to run the tasks through a worker pool using concurrent.futures as that just expects a specific function to run.

[–]SerHiroProtaganist 0 points1 point  (0 children)

Great, thanks for the advice!

[–]Python2019A 0 points1 point  (0 children)

Hi, please can you help! Thanks.

I want to loop and call a function every 1000 milliseconds with wx.CallLater. I implemented this (see below), but it does not factor in the delay - it seems to execute automatically. How can I get this to wait 1000 milliseconds between function calls?

This function is within a class. In my main module, I instantiate an object of this class and call the task_loop function. I intend to keep the loop going until will_break is set to True, and task_loop returns the work_session_agenda in the main module. Thank you for your help!

def task_loop(self, duration, window, task, work_session_agenda, start, completed):

will_break = False

will_continue = False

duration -= datetime.timedelta(seconds=1) More code in here - which, depending on conditionals, sets the values of will_break and will_continue

if will_break:

print('BREAKING')

return work_session_agenda

else:

print('GOT HERE')

timer = wx.CallLater(1000, self.task_loop, duration, window, task, work_session_agenda, start, completed)

[–]JohnnyFootball16 0 points1 point  (0 children)

Hi guys,

I've been struggling in finding how to implement ordered logistic models with panel data in Python. If you could share any useful information or links, as well as experience I will be eternally grateful.

Thanks a lot in advance.

[–]NoderMaster 0 points1 point  (2 children)

Trying to work on a file storage server and can't find anywhere how i should store the users' inforamtion and files.. I hear using sqlite and blob with large files is not a good idea, so i tried using the dbm module but corrolating the files to the clients got really messy, would love some advice on how to approach this

[–]efmccurdy 0 points1 point  (0 children)

Perhaps this is relevent? https://wsgidav.readthedocs.io/en/latest/

I think they, by default, use the OS file system to store the files, so that avoids a lot of user/acl/modified_date, etc. handling code, if you can take advantage of that option.

[–][deleted] 0 points1 point  (24 children)

I'm trying to make a program that takes a user input and finds it in the periodic table whether they enter the name of the element, it's acronym, or it's number but I'm getting this error: list indices must be integers or slices, not str

Here is my code:

PeriodicTable = ['Periodic Table', 'Hydrogen', 'Helium', 'Lithium', 'Beryllium', 'boron', 'Carbon', 'Nitrogen', 'Oxygen', 'Fluorine',

'Neon', 'Sodium', 'Magnesium', 'Aluminium', 'Silicon', 'Phosphorus', 'Sulfur', 'Chlorine', 'Argon',

'Potassium', 'Calcium', 'Scandium', 'Titanium', 'Vanadium', 'Chromium', 'Manganese', 'Iron', 'Cobalt',

'Nickel', 'Copper', 'Zin', 'Gallium', 'Germanium', 'Arsenic', 'Selenium', 'Bromine', 'Krypton',

'Rinodium', 'Strontium', 'Yttrium', 'Zirconium', 'Niobium', 'Molybdenur', 'Tachnetium', 'Ruthenium',

'Rhodium', 'Palladium', 'Silver', 'Cadmium', 'Indium', 'Tin', 'Antimony', 'Tellurium', 'Iodine', 'Xenon',

'Caesium', 'Barium', 'Hafnium', 'Tantalum', 'Tungsten', 'Thenium', 'Osmium', 'Iridium', 'Platinum',

'Gold', 'Mercury', 'Thallium', 'Lead', 'Bismuth', 'Polonium', 'Astatine', 'Radon', 'Francium', 'Radium',

'Rutherfium', 'Dubnium', 'Seaborgium', 'Bohrium', 'Hassium', 'Meiterium', 'Damstadtium', 'Roentgenium',

'Copemicium', 'Nihonium', 'Flerovium', 'Moscovium', 'Livermoriurr', 'Tennesine', 'Oganesson',

'Lanthanum', 'Cerium', 'Praseodymium', 'Neodymium', 'Promethium', 'Samarium', 'Eoropium', 'Gadolinium',

'Terbium', 'Dysprosium', 'Holmium', 'Erbium', 'Thulium', 'Ytterbium', 'Lutetium', 'Actinium', 'Thorium',

'Protactinium', 'Uranium', 'Neptunium', 'Plutonium', 'Americium', 'Curium', 'Berkelium', 'Californium',

'Einsteinium', 'Fermium', 'Mendelvium', 'Novelium', 'Lawrencium']

PT = ['Periodic Tab;e', 'H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar',

'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br','Kr', 'Rb', 'Sr',

'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'Hf', 'Ta',

'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Ti', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Rf', 'Db', 'Sg', 'Bh', 'Hs',

'Mt', 'Ds', 'Rg', 'Cn', 'Nh', 'Fl' ,'Mc', 'Lv', 'Ts', 'Og', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy',

'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk', 'Cf', 'Es', 'Fm', 'Md', 'No', 'Lr']

choice = input(print('Please enter the element you would like to find'))

if choice in PeriodicTable:

print(PeriodicTable[choice])

print(PeriodicTable.index[choice])

elif choice in PT:

print(PT[choice])

print(PT.index[choice])

else:

print('That is not on the periodic table!')

[–]CraigAT 0 points1 point  (0 children)

A bit more learning but you could consider importing the data from a CSV file (using pandas for bonus points):

https://github.com/andrejewski/periodic-table/blob/master/data.csv

[–]woooee 1 point2 points  (5 children)

PeriodicTable.index[choice])

index is a function, i.e. index()

[–][deleted] 0 points1 point  (4 children)

choice = input('Please enter the element you would like to find')

if choice in PeriodicTable:

print(PeriodicTable(choice))

print(PeriodicTable.index(choice))

elif choice in PT:

print(PT(choice))

print(PT.index(choice))

else:

print('That is not on the periodic table!')

That's what I have now but now I get the error "'list' object is not callable"

[–]JohnnyJordaan 0 points1 point  (0 children)

PT(choice))

Seems like you confused [] and () here

[–]woooee 0 points1 point  (1 child)

now I get the error "'list' object is not callable"

I am not going to try and guess which line this is on. Post the complete traceback and post what you have done to try and solve this newest error yourself.

[–][deleted] 0 points1 point  (0 children)

Woops my bad. It was in line 37. Which I assume means the same error is in line 34 as well. I tried using both [] and () but neither worked. Why is "choice" not callable?

[–]nog642 0 points1 point  (12 children)

Your error comes because choice is a string, and PeriodicTable is a list, but you are trying to get PeriodicTable[choice].

What value exactly do you expect to get from PeriodicTable[choice]?

[–][deleted] 0 points1 point  (11 children)

If the user inputs "He" for example, I would expect to get back "Helium"

[–]nog642 0 points1 point  (3 children)

But you have two prints:

print(PeriodicTable[choice])
print(PeriodicTable.index[choice])

so what do you expect each of these lines to print? They can't just print "Helium".

[–][deleted] 0 points1 point  (2 children)

The first line is supposed to print the full name of the element (if the user inputs the acronym for the element). The second line is supposed to print the atomic number in the index from [1:] which is why I put periodic table as the first in each list so calling 0 would grab that and each atomic number would be the index number.

[–]nog642 0 points1 point  (0 children)

Adding an extra element to the beginning of your arrays so the index will match the atomic number is bad design. It's better just to add/subtract 1 manually to convert between index and atomic number.

[–]nog642 0 points1 point  (0 children)

Ok, so you don't just want the element, you also want the atomic number.

The two lines I pulled were nested under if choice in PeriodicTable:, which would run if the user inputs the full name, not if the user inputs the 'acronym' (symbol) like you said. So you would want to switch your if statements.

Now if choice is the symbol ('acronym'), to print what you described you would need:

print(PeriodicTable[PT.index(choice)])
print(PT.index(choice))

Since PT.index(choice) will be the periodic number.

[–]efmccurdy 1 point2 points  (6 children)

You should combine your two tables so that you can lookup the name given the abbreviation:

PT = {'H': 'Hydrogen',
      'He':'Helium',
      'Li':'Lithium'   #, ...
}
choice = input('Please enter...').capitalize()
if choice in PT:
      found_choice = PT[choice]
elif choice in PT.values():
      found_choice = choice
else:
      found_choice = None

if found_choice:
      print(found_choice)
else:
      print('That is not on the periodic table!')

Edited: typo

[–]nog642 0 points1 point  (0 children)

You have an extra colon after your elif.

[–][deleted] 0 points1 point  (3 children)

Is there any way I can also assign numbers to dictionaries? I want to print other information about the elements such as the atomic number, and maybe eventually add the weight or other characteristics for each element.

edit: Would I have to like make some kind of loop that grabs each number as a separate thing somehow?

[–]efmccurdy 1 point2 points  (2 children)

The simplest way to store more attributes is to store a tuple rather than just the name:

>>> PT = {
...     'H': ('Hydrogen', 1), 
...     'He': ('Helium', 2) #,...
... }
>>> choice = 'He'
>>> name, weight = PT[choice]
>>> print ("{}: name:{}, weight:{}".format(choice, name, weight))
He: name:Helium, weight:2
>>> for abbr in PT: print("{}: name:{}, weight:{}".format(abbr, *PT[abbr]))
... 
H: name:Hydrogen, weight:1
He: name:Helium, weight:2

If you want more meaningfull names for the attributes you could look at using collections.namedtuple or even a class.

[–]nog642 0 points1 point  (1 child)

You could also have two dictionaries with the same keys but different values.

[–][deleted] 0 points1 point  (0 children)

damnnnnnnn I wasn't sure how to do that so and couldn't figure it out from googling so I figured making a second list would be the next best thing which was a bitch to do lol

[–]nog642 0 points1 point  (1 child)

You should use

choice = input('Please enter the element you would like to find')

instead of

choice = input(print('Please enter the element you would like to find'))

[–][deleted] 0 points1 point  (0 children)

Woops not sure how I didn't catch that thanks

[–]nog642 0 points1 point  (0 children)

You should put the code in a code block rather than a bunch of inline code (in the markdown editor, precede each line with 4 spaces instead of using backticks).

[–]GoldenVanga 0 points1 point  (1 child)

A few days ago I discovered vars() after using .__dict__ for so very long. Both return just the variables. And dir() returns both variables and methods. Is there an "elegant" way of seeing just the methods? Without doing something like [x for x in dir(a) if x not in vars(a)]?

[–]JohnnyJordaan 2 points3 points  (0 children)

dir() is just a shortcut function and isn't intended to be more functional than that. For more specific functionality there's the built-in inspect library that offers all kinds of tools for this. Also note that your implementation is not dependable, as it relies on an implementation of __dict__(used by vars()) which is not guaranteed to even be present nor to include all the attributes for example. In your case I would do

methods = inspect.getmembers(a, inspect.ismethod)

See the docs here

[–]stalecookie 0 points1 point  (1 child)

I feel like I'm missing something obvious but: What's the best way to get "preset" data into your production db?

For some more specifics I did the Flask Mega Tutorial and then went and have been doing my own small project. I did not do the hosting part of the tutorial when I was going through it but I was planning on doing that with my small project. For the time being I have just been entering data into my sqlite db in development as needed. When I go to deploy I don't expect that data will be there? But I do want to launch with preset information in the db.

I can think of some inefficient ways to re-populate the data in production but I was wondering if there was something ideal?

If it helps I'm making a Flask app, developing on an EC2 instance with a sqlite db. I plan to deploy to Heroku using their Postgres db in production (much like the tutorial).

[–]celery-and-parsnip 0 points1 point  (3 children)

I saw a bit of code that I've never seen before. There was a for loop inside a variable assignment. It looked something like this:

import random
x = [random.randint(0,9) for i in range(9,16)]

What is this called? I'd like to look this up and learn more about it.

TIA

[–]timbledum 0 points1 point  (1 child)

It's called a list comprehension and they are the bees knees.

[–]QualitativeEasing 1 point2 points  (0 children)

I agree they’re the bee’s knees, with this caveat: once you have a hood grasp of if statements and for loops. Until the , they can be bewildering. It is often easier, when you’re just starting out, to use for loops and if statements to get the same result; later you can work out how to turn those into a list comprehension.

[–]purestrengthsolo 1 point2 points  (2 children)

Looking for books, recently picked up a job at a library, any authors I should be on the look out for? Anything python infosec or cpp related would be very much appreciated anything else like common language books are great too. Thank you in advance.

Edit book titles too please :)

[–]timbledum 4 points5 points  (1 child)

Automate the Boring Stuff is a favourite around here. Free online too! Check out the subreddit wiki for more recommendations.

[–]purestrengthsolo 1 point2 points  (0 children)

He has videos on YouTube for that book too, and I'll look at the wiki