|
Any idea how I can get python to read and show umlaut characters from a file properly? My file is called data and contains only: ö My python program: import string, msvcrt, sys myfile=open("data") print myfile.readlines() When I run it, I get (in the XP command prompt) C:\Python\test>test.py ['\xf6'] Replacing the umlaut with a plain x in the "data" file works OK: C:\Python\test>test.py ['x'] I suspect it's not really python but XP because of this: C:\Python\test>type data ÷ But I can type in umlauts on the prompt: C:\Python\test>ööööääääää 'ööööääääää' is not recognized as an internal or external command, operable program or batch file. Any ideas? Thanks.
|
# ¿ Feb 17, 2008 16:46 |
|
|
# ¿ May 4, 2024 16:15 |
|
outlier posted:Not Vista's fault, not Python's fault. You're got a non-ascii file. Aha! Thanks very much. After living in Switzerland all this time I've become so used to umlauts that I forgot they are non-ASCII.
|
# ¿ Feb 17, 2008 21:15 |
|
On Windows XP I'm trying to get the output from a command using the subprocess module in Python 3.0 The subprocess docs say this: output=`mycmd myarg` ==> output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0] So I try it: >>> output = subprocess.Popen(["a.bat"], stdout=PIPE).communicate()[0] Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'PIPE' is not defined I then tried getoutput: >>> subprocess.getoutput("a.bat") "'{' is not recognized as an internal or external command,\noperable program or batch file." What's going on? How do you guys get the output from a command back into Python? Thanks.
|
# ¿ Dec 25, 2008 20:57 |
|
Janin posted:Looks like a typo in the docs: try stdout=subprocess.PIPE Unfortunately no dice... >>> output = subprocess.Popen(["a.bat"], stdout=subprocess.PIPE).communicate()[0] Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'subprocess' is not defined
|
# ¿ Dec 25, 2008 22:24 |
|
Thanks. Dammit how can I forget to import. Yeah, these are my first few hours using Python. Mmmm... so what I get back is a big bunch of bytes/characters. I was hoping for something easier to deal with. Back to the drawing board.
|
# ¿ Dec 25, 2008 22:50 |
|
tripwire posted:What kind of bytes/characters? What are you expecting the output to be? Python has functionality for switching between character encodings if that's the problem. I've just starting learning Python and my current learning target is to use the logging module to make one nice logfile for everything my script does. So I want to get the output of external commands run in my script and shove them into the logfile. a.bat: @echo off echo Output from external command line 1 echo Output from external command line 2 >>> output = subprocess.Popen("a.bat", stdout=subprocess.PIPE).communicate()[0] >>> print(output) b'Output from external command line 1\r\nOutput from external command line 2\r\n' I guess I can do this but the output is not tidy: >>> for i in str(output).split("\\r\\n"): ... print(i) b'Output from external command line 1 Output from external command line 2 ' Looking around the docs I found this: >>> output=os.popen("a.bat") >>> for i in output.readlines(): ... print(i) ... Output from external command line 1 Output from external command line 2 Apart from the extra blank lines this is what I need so I can go with this. It seems the subprocess module is the 'proper' way to do this but I can't get it to work nicely...
|
# ¿ Dec 26, 2008 12:17 |
|
ChiralCondensate posted:Did you try calling split() directly on the bytes object? (I don't know if it has such a method.) TypeError: Type str doesn't support the buffer API ChiralCondensate posted:
Aaaahaaaa! That's what I wanted! Thanks! (Now I'll read up on this encoding/decoding stuff.)
|
# ¿ Dec 26, 2008 15:59 |
|
|
# ¿ May 4, 2024 16:15 |
|
jstirrell posted:Ok so I'm trying to write a program which basically allows me to input questions and their answers, organized by chapter/book/whatever so I can study them. My first Python program was pretty much this. I used it to learn German vocabulary. I stored everything in text files like so: english:german english:german A different text file for each topic and made a menu in Python to load whichever file I wanted. Then it would show a random English word and then after a keypress the corresponding German word.
|
# ¿ Jan 3, 2009 21:03 |