2

I am using a windows7 machine and I cannot figure out why Python 3 is writing this character at the end of each line ਍ഀ

Here is my code:

import csv

input_file = open('7.15.16.txt', 'r')
output_file = open('no_commas1.csv', 'w')
for line in input_file:
    line = line.replace(",", " ")
    output_file.write(line)
output_file.close()
input_file.close()
specmer
  • 389
  • 1
  • 5
  • 14
  • Only thing I can think of: check the encoding, you can trying opening the file with a specific encoding too: f = open(fname, encoding="utf-8"). – Dese Jul 18 '16 at 12:50
  • I believe those are [newline symbols](https://en.wikipedia.org/wiki/Newline) '\r\n'. Which problems it cause? Do you have them in original file? – Alena Kastsiukavets Jul 18 '16 at 12:51
  • They are not in the original file. I also cannot open the file with encoding="utf-8" - I receive an error. – specmer Jul 18 '16 at 12:52

2 Answers2

1

Ok, you have a .txt file. So to define a new line in .txt file \r\n symbols are used. You will not see them if you open it in text editor. You have to turn on invisible characters mode. You can use notepad++ to see them.

Then you save information in .csv file. But .csv does not have a special meaning for \r\n symbols. As a result, they are displayed as is.

You have to remove \r\n symbols in your code directly. See this issue

Community
  • 1
  • 1
-1
input_file = open('7.15.16.txt', 'r', encoding='utf16')
Joundill
  • 6,828
  • 12
  • 36
  • 50
Jason
  • 1