0

I checked it in a lot ways, and always i have same problem with coding. I have text file, where ; is a separator. I'd like to input all splitted data into python to work on it. There's my code:

f = open("zestaw.txt")
od = f.read()
od = od.splitlines()
f.close()
print od[0]
output =[]
for p in od:
    output.append(p.split(';'))
print output[0]

first print gives me a normal output without coding problems and its looks like:

Tytuł;Autor;ISBN;Wydawnictwo;Rok wydania;Kategoria;Podkategoria

But second print

['Tytu\xb3', 'Autor', 'ISBN', 'Wydawnictwo', 'Rok wydania', 'Kategoria', 'Podkategoria']

Everything is good expect changing ł for a \xb3

Anyone have idea how to change it? only python 2.7

Dziabaduch
  • 23
  • 3
  • 2
    No, that's exactly the same encoding. Python lists show their contents as *representations*, source code you can copy back into Python to reproduce the value. – Martijn Pieters May 28 '17 at 12:30
  • 2
    The syntax uses *ASCII safe notations*, to avoid issues with different encodings. `ł` is not an ASCII character, so Python uses a `\xhh` escape sequence to show you the exact byte value. If you pasted `'Tytu\xb3'` into Python you'd get the exact same value again. Try `print 'Tytu\xb3'`. – Martijn Pieters May 28 '17 at 12:31
  • 1
    In other words, there is nothing to change here. For example, `print output[0][0]` will print `Tytuł ` again, provided your console is still using the same locale. – Martijn Pieters May 28 '17 at 12:32
  • @Gahan: come again? Why would that make any difference here? – Martijn Pieters May 28 '17 at 12:34
  • So there isn't any way to have list written correctly in my language? – Dziabaduch May 28 '17 at 12:37
  • because text file can be encoded in many ways but web page may or may not support that kind of encoding. in any case if he don't want to share sample text file then it at least requires to know which encoding style is used to store content – Gahan May 28 '17 at 12:38
  • @Dziabaduch: All Python containers (list, dict, set, tuple) show output like that. If you need different output, you need to format it yourself. – Martijn Pieters May 28 '17 at 12:44
  • 1
    @Gahan: sure, but that doesn't make your comment *you need to provide text file for that* make any more sense. – Martijn Pieters May 28 '17 at 12:44
  • this might be helpful : https://stackoverflow.com/questions/491921/unicode-utf-8-reading-and-writing-to-files-in-python – Gahan May 28 '17 at 12:55

0 Answers0