G
G
gadzhi152016-08-11 12:12:42
Python
gadzhi15, 2016-08-11 12:12:42

csv file encoding. How to change?

There is a csv file in windows-1251 encoding. In Python, I use the pandas library to work with this file. There are problems when deleting Cyrillic characters. How to change encoding using Python to utf-8?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
polar_winter, 2016-08-11
@gadzhi15

Offtop in Linux - man iconv

S
sim3x, 2016-08-11
@sim3x

In [1]: with open('/tmp/1', 'w+', encoding='cp1251') as f: f.write('qwertyйцукен')

In [2]: cat /tmp/1
qwerty������
In [3]: with open('/tmp/1', 'r', encoding='cp1251') as f: print(f.read())
qwertyйцукен

In [4]: with open('/tmp/1', 'r', encoding='cp1251') as f: 
   ...:     with open('/tmp/2', 'w+', encoding='utf-8') as o: o.write(str(f.read()))
   ...:     

In [5]: cat /tmp/2
qwertyйцукен

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question