r/learningpython • • Jul 03 '19

Automatically download File from website on a monthly basis

1 Upvotes

I am currently trying to work out how to automatically download a file from an URL every month using python. It is a CSV document with that month's data so for example in January 2019 the link would be:

http://thiswebsite.com/documents/2019/jan%19.csv.

Is it possible to automatically update both the year and month in the link. I am using Urllib to download the file.


r/learningpython • • Jan 07 '19

I want this lope to run until commanded to stop by the press of a button how do I prematurely stop the loop before it reaches the end of the range

Post image
1 Upvotes

r/learningpython • • Nov 05 '18

Converting a string into its value? (Doubt that's the correct terminology)

1 Upvotes

Without explaining all my code, this is a simple version of my issue.

x = 1

y = 2

z = 3

list = ["x","y","z"]

result = random.choice(list)

How do i now convert "result" into a number from this?

Thank you!


r/learningpython • • May 25 '18

Encoding to stdout

1 Upvotes

Dear Redditors

I have an issue displaying chinese characters in the terminal, bellow is a simple script to illustrate this issue (using python 2.7.9 on Windows 8.1):

# -*- coding:Utf-8 -*-
a=u"你好"

try:
	print "print a"
	print a
except Exception, e:
	print e
print

try:
	print "print a.encode('utf-8')"
	print a.encode('utf-8')
except Exception, e:
	print e
print

import sys
try:
	print "sys.stdout.encoding:",sys.stdout.encoding
	print "print a.encode("+sys.stdout.encoding+")"
	print a.encode(sys.stdout.encoding)
except Exception, e:
	print e
print

import locale
try:
	print "locale.getpreferredencoding():",locale.getpreferredencoding()
	print "print a.encode("+locale.getpreferredencoding()+")"
	print a.encode(locale.getpreferredencoding())
except Exception, e:
	print e

When running this directly in the terminal, I get:

print a
'charmap' codec can't encode characters in position 0-1: character maps to <undefined>

print a.encode('utf-8')
õ¢áÕÑ¢

sys.stdout.encoding: cp850
print a.encode(cp850)
'charmap' codec can't encode characters in position 0-1: character maps to <undefined>

locale.getpreferredencoding(): cp1252
print a.encode(cp1252)
'charmap' codec can't encode characters in position 0-1: character maps to <undefined>

When running the same from IDLE, I get:

print a
你好

print a.encode('utf-8')
你好

sys.stdout.encoding: cp1252
print a.encode(cp1252)
'charmap' codec can't encode characters in position 0-1: character maps to <undefined>

locale.getpreferredencoding(): cp1252
print a.encode(cp1252)
'charmap' codec can't encode characters in position 0-1: character maps to <undefined>

I would like to have a way to reliably display those two characters, either in terminal, either in IDLE.

Beside, I don't understand why I can't get it to be displayed in the terminal:

  • the variable a is an unicode object

  • it should be properly decoded from UTF-8 (thanks to the file first line encoding declaration)

  • encoding into the same encoding as stdout should do the trick no?

And I don't really understand why the print a (no encoding) statement won't works in the terminal, but works IDLE. Any idea?

If neither the command line nor IDLE stdout use utf-8 as encoding, while encoding to utf-8 won't fail when using print a.encode('utf-8') ?

Thanks!


r/learningpython • • Apr 04 '18

trying to get 2048 to play by itself

1 Upvotes

I'm trying to get a game of 2048 to play by itself by sending keystrokes to it. I can get it to play 1 iteration but something is wrong with my nested for loop. I want it to send UP RIGHT DOWN LEFT. I'm not necessarily looking for someone to solve this problem for me but tell me where my problem is and maybe some guidance on why my nested loop isn't working correctly.

import webbrowser
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox(executable_path=r'C:\~my driver location~\geckodriver.exe')
browser.get('https://gabrielecirulli.github.io/2048/')
htmlElem = browser.find_element_by_tag_name('html')
moves = [htmlElem.send_keys(Keys.UP), htmlElem.send_keys(Keys.RIGHT), 
htmlElem.send_keys(Keys.DOWN), htmlElem.send_keys(Keys.LEFT)] 
for turns in range(100):
    for i in range(4):
    	moves[i]

Edit #1: formatting

Edit #2: I got it working

import webbrowser
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Firefox(executable_path=r'C:\Users\toby.hoyle\Downloads\geckodriver.exe')
browser.get('https://gabrielecirulli.github.io/2048/')
htmlElem = browser.find_element_by_tag_name('html')

for turns in range(100):
    for i in range(4):
    	moves = [htmlElem.send_keys(Keys.UP), htmlElem.send_keys(Keys.RIGHT), 
htmlElem.send_keys(Keys.DOWN), htmlElem.send_keys(Keys.LEFT)] 

    	moves[i]
    	print(str(turns) + ' - ' + str(i))

r/learningpython • • Jan 25 '18

Trying to figure out writing to specific columns in a csv

Thumbnail
stackoverflow.com
1 Upvotes

r/learningpython • • Dec 29 '17

Logging into site and downloading csv (complete beginner)

1 Upvotes

Hi!

I've only just started to learn python and am trying to do small projects on the side of the (somewhat boring) lectures I'm following.

I'm trying to use requests to first log into a website, and then to download a CSV through a URL that's linked in the UI of that site once you're logged in.

I found several variations on how to log in through python, and have been successful at that, but I have been unable to find guidance for how to download, read and store the CSV.

Here's my script for logging in:

import requests

with requests.Session() as c:

url = 'https://example.com/admin/'
username = "myusername"
mypass = "mypassword"
c.get(url)
login_data = dict(login=username, password=mypass)
c.post(url, data=login_data, headers={"Referer": "https://example.com/admin/"})
page = c.get('https://example.com/admin/index.php?csv=true&id=1337&setController=admin')

So the site is obviously example.com/admin and the site has a specific URL for downloading a csv that I'm interested in.

I would greatly appreciate any and all guidance! (disclaimer: I apologize if this is out of scope for this subreddit)


r/learningpython • • Dec 15 '17

Selecting a date range in a Panda dataframe

1 Upvotes

Still new at this... I have a directory with 20 csv files, each containing data for 5 days' worth of data or so. The first column is the day (2017-10-16, for instance).

I can read the files into a dataframe, but now I would like to keep only the data for 1 specific day. I can't seem to get it... Anyone with an example?

TIA!


r/learningpython • • Aug 31 '17

GitHub - PyPiPie/Scarlet-1.0-Virtual-Assistant-Made-With-Python: S.C.A.R.L.E.T (Sorta Crappy Assistant Robot Lazily Engineered Today)

Thumbnail
github.com
1 Upvotes

r/learningpython • • May 13 '17

url.replace("learning", "learn")

Thumbnail
reddit.com
1 Upvotes

r/learningpython • • Aug 10 '16

moving a file from an sftp site to an ftp site

1 Upvotes

Hello everyone and thanks in advance for any help. I am just starting to pick up python and have a project where I am wanting to take the newest file in a sub-directory of an sftp site(port 22 no key) and move it to a sub-directory on a different ftp server(ftp over TLS) renaming it in the process from "new-file2016.txt" to "useme2016.txt".

I have used paramiko and successfully connected to my ftp site but am pretty lost after that.