I have a text file which contains TAB between values and looks like this:
Yellow_Hat_Person 293 997 328 1031
Yellow_Hat_Person 292 998 326 1032
Yellow_Hat_Person 290 997 324 1030
Yellow_Hat_Person 288 997 321 1028
Yellow_Hat_Person 286 995 319 1026
I want to replace all the tabs with just a single space. so it looks like this:
Yellow_Hat_Person 293 997 328 1031
Yellow_Hat_Person 292 998 326 1032
Yellow_Hat_Person 290 997 324 1030
Yellow_Hat_Person 288 997 321 1028
Yellow_Hat_Person 286 995 319 1026
Any suggestions would be of help.
You need to replace every '\t'
to ' '
inputFile = open(“textfile.text”, “r”)
exportFile = open(“textfile.txt”, “w”)
for line in inputFile:
new_line = line.replace('\t', ' ')
exportFile.write(new_line)
inputFile.close()
exportFile.close()
It would be better to use correct quote chars, and don't make input & output file names very similar. Based on @mooga's answer, please use this:
fin = open("input.txt", "r")
fout = open("output.txt", "w")
for line in fin:
new_line = line.replace('\t', ' ')
fout.write(new_line)
fin.close()
fout.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With