Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
dwayne_dibbley
Nov 26, 2005
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.

Adbot
ADBOT LOVES YOU

dwayne_dibbley
Nov 26, 2005

outlier posted:

Not Vista's fault, not Python's fault. You're got a non-ascii file.

Open the file with `codecs.open()`, while specifying the encoding you want and then treat it like a normal 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.

dwayne_dibbley
Nov 26, 2005
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.

dwayne_dibbley
Nov 26, 2005

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

dwayne_dibbley
Nov 26, 2005
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.

dwayne_dibbley
Nov 26, 2005

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...

dwayne_dibbley
Nov 26, 2005

ChiralCondensate posted:

Did you try calling split() directly on the bytes object? (I don't know if it has such a method.)
No, there is no method:
TypeError: Type str doesn't support the buffer API

ChiralCondensate posted:

code:
>>> for i in output.decode().split("\\r\\n"):
...     print(i)

Aaaahaaaa! That's what I wanted! Thanks! (Now I'll read up on this encoding/decoding stuff.)

Adbot
ADBOT LOVES YOU

dwayne_dibbley
Nov 26, 2005

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.

  • Locked thread