Ich habe Python 2.7.3 und Python 3.2.3 auf Ubuntu 12.04. Zum Testen lese ich eine kleine Textdatei in eine Liste. Mit Python 3 geht das ohne Probleme:
Code: Alles auswählen
#!/usr/bin/env python3
with open("wordsDE.txt") as my_file:
lines = my_file.readlines() # read lines into a list
my_file.close()
print(lines)
- ['ührchen\n', 'ähre\n', 'öffnen\n', '\n']
Code: Alles auswählen
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import codecs
#my_file = open('wordsEN.txt', 'r') # ok
with codecs.open("wordsDE.txt", "r", encoding = "utf-8") as my_file:
lines = my_file.readlines() # read lines into a list
my_file.close()
print lines
- [u'\xfchrchen\n', u'\xe4hre\n', u'\xf6ffnen\n', u'\n']
Danke,
siggi