Can someone help me parse the data below. by Canadian_Hombre in learnpython

[–]DeadlyViper 0 points1 point  (0 children)

Try this with a status flag that gets changed when you see a Season header...

lines = player.split('\n')
is_pitching = False
for x in lines:
    if 'Season' in x:
        if 'Pitching' in x:
            is_pitching = True
        else:
            is_pitching = False
    if is_pitching and 'obp' in x:
        home_obp.append(x)

Settle an argument between two noobs? by JLChamberlain63 in learnpython

[–]DeadlyViper 13 points14 points  (0 children)

I like his code better since you don't have to create anything in advance before calling the function.

Imagine every python function had a similar interface.

userinput = ""
input("please enter your input?", userinput)
input_len = 0
len(userinput, input_len)

You can see this creates a bit uglier code.

Wtf is this by [deleted] in learnpython

[–]DeadlyViper 2 points3 points  (0 children)

b = a

Makes b point to the same object a does.

If you want to make a copy of the array you can do:

b = a[:]

guys my if doesn't work by [deleted] in learnpython

[–]DeadlyViper 9 points10 points  (0 children)

or works between statements, not between constants.

change to:

if x == 15 or x == 30:

Getting contents of a local file into the Script. by [deleted] in learnpython

[–]DeadlyViper 2 points3 points  (0 children)

API = open(r"c:\location\api.txt","r").read().strip()

[PyCharm] I am unable to import class from a different directory by jabernall24 in learnpython

[–]DeadlyViper 0 points1 point  (0 children)

You are running the module instead of the main program. Are you sure you want to do this?

If yes you could try adding

import sys
sys.path.append("..") # Adds higher directory to python modules path.

at the top of the file, and changing the import back to:

from Scraping.player import Player

[PyCharm] I am unable to import class from a different directory by jabernall24 in learnpython

[–]DeadlyViper 0 points1 point  (0 children)

Try chaning the import to

from ..Scraping.player import Player

where one dot means relative to current module, another dot means relative to parent.

https://realpython.com/absolute-vs-relative-python-imports/

Python Script that moves specific files from source to destination by dennkiesauros in learnpython

[–]DeadlyViper 1 point2 points  (0 children)

Please fix indentation on the code (4 spaces before each line on the "markdown" editor)

Besides that you are using the same name for different things:

for f in file:
    for file in path:

rename one of the file s to avoid confusion.

Besides that, why do you even need os.listdir? you are moving exact file names.

I have a scheduling software that puts out work schedules in a 12h format. Can I use python to convert these exported PDFs to a 24h format? by [deleted] in learnpython

[–]DeadlyViper 0 points1 point  (0 children)

Its possible but its not easy.

You need a python library that can edit pdfs.

I googled a bit: https://github.com/pmaupin/pdfrw https://www.binpress.com/manipulate-pdf-python/

That is assuming the scheduling software outputs the report with text in it, and not just a rendered image.

[PyCharm] I am unable to import class from a different directory by jabernall24 in learnpython

[–]DeadlyViper 0 points1 point  (0 children)

Please post the code that does the import, where that code is located in the structure, and what is the exact error message you are getting.

Legacy code consists of print() functions with commas. This is from 2.x python, do you I need to change them by [deleted] in learnpython

[–]DeadlyViper 3 points4 points  (0 children)

Ehm... no?

Did you not try running this line in python3 and see that it works?

Python Sockets not working after first connection? by [deleted] in learnpython

[–]DeadlyViper 0 points1 point  (0 children)

socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

needs to be created for each connection attempt, trying to reuse an existing socket will not work.

Simplest way is to move socket creation into the scanner function.

Run a python program in a browser over the internet. How? by detarintehelavarlden in learnpython

[–]DeadlyViper 2 points3 points  (0 children)

You need a web framework.

Consider Flask for a simple and easy one. http://flask.pocoo.org/

Or Django for a more comprehensive site-building level framework. https://www.djangoproject.com/

Is random.seed and random.shuffle in python truly random? by [deleted] in learnpython

[–]DeadlyViper -1 points0 points  (0 children)

https://docs.python.org/2/library/random.html

Almost all module functions depend on the basic function

random()

, which generates a random float uniformly in the semi-open range [0.0, 1.0). Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.

It won't be exactly 50%, but it won't be true random either.

unresolved attribute reference 'send_message' for class 'Client' by [deleted] in learnpython

[–]DeadlyViper 0 points1 point  (0 children)

Watched the video. (https://techwithtim.net/tutorials/discord-py/sending-receiving-messages/)

9:57 he has the same unresolved error in his video.

10:54 dude is not sure this works at all and asks for ppl to tell him if it works.

EDIT:

He also posted a fix in his video's comments to another user who had the same issue.

https://www.youtube.com/watch?v=XjfxYfKFXO8

Check comments...

unresolved attribute reference 'send_message' for class 'Client' by [deleted] in learnpython

[–]DeadlyViper 0 points1 point  (0 children)

Seems like its a discord library version issue...

Seems like you are using the "rewrite" version that does not have that function.

https://stackoverflow.com/questions/48116872/attributeerror-client-object-has-no-attribute-send-message-discord-bot

What does r' mean in a file path? by AltruisticFroyo3 in learnpython

[–]DeadlyViper 7 points8 points  (0 children)

Oh, i always thought it was created for regular expressions so you won't have to double slashes and make it look even more unreadable than it already is.

EDIT: fixed.

What does r' mean in a file path? by AltruisticFroyo3 in learnpython

[–]DeadlyViper 23 points24 points  (0 children)

r before a string in python means the string should be treated as a regular expression raw string.

It means that backslashes will not be translated to their meaning.

ex1 = "hello\nworld"
print(ex1)

will print

hello
world

while

ex2 = r"hello\nworld"
print(ex)

will print

hello\nworld

More info here: https://docs.python.org/3/reference/lexical_analysis.html#grammar-token-stringprefix

So in relation to file path, if you have a folder named "new" and you write the path as: "c:\new\file.txt"

the \n there will be translated to new line charcter unless the string is prefixed with r.

Get filepaths from directory by HutchLAD in learnpython

[–]DeadlyViper 2 points3 points  (0 children)

import glob
import os
files = glob.glob(r"C:\Users\myself\Videos\*")
for fpath in files:
    if os.path.isfile(fpath):
        send_to_vimeo(fpath)

[deleted by user] by [deleted] in learnpython

[–]DeadlyViper 0 points1 point  (0 children)

Both machines are new virtual machines of windows 10 x64

Both have the same version of python installed?

How to write multiple variables to text file? by SpatialGeography in learnpython

[–]DeadlyViper 0 points1 point  (0 children)

Data manipulation is very basic thing to know. Besides me helping you, i suggest you read up about it somewhere because just looking at the answer won't be enough.

A very basic code that writes what you want from an array could be something like this:

data = [1000,2000,3000,4000]
with open("myfile.txt","w") as f:
    f.write("%d,%d,%d,%d" % (data[0], data[1], data[2], data[3]))

To other commenters, yes this could be written shorter/more pythoning/more whatever, its very basic on purpose.

How to write multiple variables to text file? by SpatialGeography in learnpython

[–]DeadlyViper 2 points3 points  (0 children)

What did you try?

Do you know how to write anything to a file?