1

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
asked May 29, 2016 at 13:43
1
  • what line does the error point to? Commented May 29, 2016 at 16:21

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

8 Comments

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???
Are you sure you have tabs and not just spaces? If you print(repr(line)) what do you see?
It works now i needet to change the \t to \n. Thx a lot now it totally works :D
is there a reasson why it don't work with the regex cause i just tested the same with regex and it does nothing.
What are you expecting \R to change?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.