I am trying to replace all tabs with a space so I can put my Coma seperated text in one line in another file. Now my code look like this:
from __future__ import print_function
import re
import ast
f = open('sample_test.txt', 'r')
g = open('sample_test1.txt', 'w')
for line in f:
c = re.sub(r'\R', r' ', line.rstrip())
print (c, file = g)
f.close()
The problem now is that I get this error:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 1944: character maps to <undefined>
Ian McLaird
5,5852 gold badges25 silver badges31 bronze badges
-
what line does the error point to?Tadhg McDonald-Jensen– Tadhg McDonald-Jensen2016年05月29日 16:21:19 +00:00Commented May 29, 2016 at 16:21
1 Answer 1
Open the files as utf-8, you also don't need a regex if you just want to replace tabs:
import io
with io.open('sample_test.txt', encoding="utf-8") as f, io.open('sample_test1.txt', 'w', encoding="utf-8") as g:
for line in f:
g.write(line.replace("\t"," "))
Tadhg McDonald-Jensen
21.6k5 gold badges40 silver badges69 bronze badges
answered May 29, 2016 at 15:47
Padraic Cunningham
181k30 gold badges264 silver badges327 bronze badges
Sign up to request clarification or add additional context in comments.
8 Comments
Vedad
Thx that solves the Problem with the error, but for some reasson it don't replace the tabs with an empty space. The regex also don't work. Any Idea while it just copy the same content to g???
Padraic Cunningham
Are you sure you have tabs and not just spaces? If you
print(repr(line)) what do you see?Vedad
It works now i needet to change the \t to \n. Thx a lot now it totally works :D
Vedad
is there a reasson why it don't work with the regex cause i just tested the same with regex and it does nothing.
Padraic Cunningham
What are you expecting
\R to change? |
lang-py