python-3.3

Unicode error - opening *.txt files with python

ぐ巨炮叔叔 提交于 2019-12-12 19:40:10
问题 When I try to read a text file like so in python: x = open("C:\Users\username\Desktop\Hi.txt", 'r') This error is returned: SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape I looked around and found this question: "Unicode Error "unicodeescape" codec can't decode bytes... Cannot open text files in Python 3. Apparently I need to duplicate all of the backslashes so Unicode doesn't get all screwed up by what I am trying to do. So

How to delete everything after a certain character in a string?

放肆的年华 提交于 2019-12-12 07:47:28
问题 How would I delete everything after a certain character of a string in python? For example I have a string containing a file path and some extra characters. How would I delete everything after .zip? I've tried rsplit and split , but neither included the .zip when deleting extra characters. Any suggestions? 回答1: Just take the first portion of the split, and add '.zip' back: s = 'test.zip.zyz' s = s.split('.zip', 1)[0] + '.zip' Alternatively you could use slicing, here is a solution where you

AttributeError: 'module' object has no attribute 'request'

大憨熊 提交于 2019-12-12 07:29:12
问题 When I run the following code in Python 3.3: import urllib tempfile = urllib.request.urlopen("http://yahoo.com") I get the following error: I did this too to verify: What am I doing wrong? 回答1: Import urllib.request instead of urllib . import urllib.request 回答2: Interestingly, I noticed some IDE-depending behavior. Both Spyder and PyCharm use the same interpreter on my machine : in PyCharm I need to do import urllib.request while in Spyder, import urllib does fine 回答3: If this is on PyCharm,

How can I add to the tally of a chosen phrase?

旧巷老猫 提交于 2019-12-12 04:19:09
问题 So I want to be able to add to the tally of a certain phrase that is contained in a file. However I have no clue on where to start. Let me explain a little more: I have a .txt file that is called setPhrases.txt . This file contains this with in it: What is your name?, 9 What time is dinner?, 8 Thank-You., 9 I have done all my homework., 7 Can you bring me a drink Please?, 6 Hi my name is Dave., 10 I am currently able to take (n) number of the top phrases (So the ones with the highest count)

my tkinter gui is invisible for unknown reasons

六月ゝ 毕业季﹏ 提交于 2019-12-11 23:24:53
问题 I've been trying to create a basic gui using tkinter, I've done it before on a different computer but for some reason its invisible. Is there something wrong with my code or the computer (windows)? import sys from tkinter import * mygui = Tk() mygui.geometry('300x300+0+982') mygui.title("my gui") mygui.mainloop() 回答1: Is your screen height is bigger than 982 pixel? Following line place the window at (0, 982) with width 300, height 300. If your screen height smaller than 982 pixel, you can't

Python3 - ascii/utf-8/iso-8859-1 can't decode byte 0xe5 (Swedish characters)

一个人想着一个人 提交于 2019-12-11 21:23:15
问题 I've tried io , repr() etc, they don't work! Problem inputting å ( \xe5 ) : (None of these work) import sys print(sys.stdin.read(1)) sys.stdin = io.TextIOWrapper(sys.stdin.detach(), errors='replace', encoding='iso-8859-1', newline='\n') print(sys.stdin.read(1)) x = sys.stdin.buffer.read(1) print(x.decode('utf-8')) They all give me roughly UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe5 in position 0: unexpected end of data Also tried starting Python with: export PYTHONIOENCODING=utf

Simple:Python asks for input twice

风流意气都作罢 提交于 2019-12-11 14:34:29
问题 Here is my code: def calculator(value1,value2): function=input("Function?") if function=="*": return value1*value2 if function=="/": return value1/value2 if function=="+": return value1+value2 if function=="-": return value1-value2 a=float(input("value 1:")) b=float(input("value 2:")) calculator(a,b) print(calculator(a,b)) Output on Python Shell value 1:5 value 2:5 Function?/ Function?/ 1.0 So im just wondering why it asks for input for function twice, not once. This is probably a stupid

Import error with module _ssl for Python 3.3

醉酒当歌 提交于 2019-12-11 13:23:06
问题 I am programming in Python 3.3 on the latest version of Ubuntu. I am writing code for a project involving a library that works with Twitter. Here is the output from the terminal I have: Traceback (most recent call last): File "tryout.py", line 2, in <module> import twitter File "/home/owner/Documents/twitter/__init__.py", line 15, in <module> from .stream import TwitterStream File "/home/owner/Documents/twitter/stream.py", line 9, in <module> from ssl import SSLError File "/usr/local/lib

Destroying a dynamically created widget

拜拜、爱过 提交于 2019-12-11 10:18:03
问题 I have basically a similar question, though I do not feel it has been answered properly: Tkinter: How can I dynamically create a widget that can then be destroyed or removed? The accepted answer is: You'll want to store the dynamically-created widgets in a list. Have something like dynamic_buttons = [] def onDoubleClick(event): ... button = Button(...) dynamic_buttons.append(button) button.pack() You can then access the buttons for removal with, say, dynamic_buttons[0].destroy() You can see

Python 3.3 HTTP Cookie Error

随声附和 提交于 2019-12-11 09:12:33
问题 I just did a fresh install (from source) of Python 3.3 and I installed the 'requests' library. I'm attempting a very simple request: r = requests.get('https://www.google.com/') And I'm getting a very strange error in return: File "/usr/local/lib/python3.3/http/cookiejar.py", line 1647, in extract_cookies if self._policy.set_ok(cookie, request): File "/usr/local/lib/python3.3/http/cookiejar.py", line 931, in set_ok if not fn(cookie, request): File "/usr/local/lib/python3.3/http/cookiejar.py",