Δημοσιεύτηκε: 19 Νοέμ 2010, 09:46
από medigeek
Δεν κάνει τίποτα. :) Ήμουν έτοιμος να σου πω διάβασε το σύνδεσμο που έδωσα προηγουμένως: http://docs.python.org/howto/unicode.ht ... icode-type
Αλλά για να μην ζορίζεσαι, μιας και αναφέρει κάτι στις παραγραφους για το utf-8 -- η απάντηση είναι (αν το αρχείο είναι utf-8): unicode( "text string", "utf-8")
Κώδικας: Επιλογή όλων
#!/usr/bin/python
# -*- coding=utf-8 -*-
file_obj = open('a.txt', 'a+') #anoigei to arxeio gia anagnosi 'h eisagwgh stoixeion
alltext_from_file = file_obj.read()
unicoded_string = unicode(alltext_from_file, "utf-8")
file_obj.close()

print(unicoded_string)
for letter in unicoded_string:
print(letter)


έγραψε:>>> file_obj = open('a.txt', 'ra')
>>> alltext_from_file = file_obj.read()
>>> unicoded_string = unicode(alltext_from_file, "utf-8")
>>> file_obj.close()
>>> print(alltext_from_file)
τεστ τεστ

>>> alltext_from_file
'\xcf\x84\xce\xb5\xcf\x83\xcf\x84 \xcf\x84\xce\xb5\xcf\x83\xcf\x84\n'
>>> unicoded_string
u'\u03c4\u03b5\u03c3\u03c4 \u03c4\u03b5\u03c3\u03c4\n'

>>> for letter in unicoded_string:
... print(letter)
...
τ
ε
σ
τ

τ
ε
σ
τ