1

I import a .csv file that looks like:

enter image description here

by using the following code:

filetoread = 'G:/input.csv'
filetowrite = 'G:/output.csv'

# https://stackoverflow.com/questions/17658055/how-can-i-remove-carriage-return-from-a-text-file-with-python/42442131

with open(filetoread, "rb") as inf:
    with open(filetowrite, "wb") as fixed:
        for line in inf:
            # line = line.replace('\r\r\n', 'r\n')
            fixed.write(line)
            print(line)

Which give the output:

b'\xef\xbb\xbfHeader1;Header2;Header3;Header4;Header5;Header6;Header7;Header8;Header9;Header10;Header11;Header12\r\n'
b';;;1999-01-01;;;01;;;;;;\r\n'
b';;;2000-01-01;;;12;;"NY123456789\r\r\n'
b'";chapter;2020-01-01 00:00:00;PE\r\n'
b';;;2020-01-01;;;45;;"NY123456789\r\r\n'
b'";chapter;1900-01-01 00:00:00;PE\r\n'
b';;;1999-01-01;;;98;;;;;;\r\n'

I have issues to replace \r\r\n to \r\n which I guess I need to do to get my desired output. The error I get when I try to replace the \r\r\n is:

Traceback (most recent call last):
  File "g:/till_format_2.py", line 10, in <module>
    line = line.replace('\r\r\n', 'r\n')
TypeError: a bytes-like object is required, not 'str'

My desired output:

enter image description here

What do I need to add or change to the code to achieve my desired output?

Wizhi
  • 6,424
  • 4
  • 25
  • 47

1 Answers1

2

As the error message says, supply a bytes object.

line = line.replace(b'\r\r\n', b'\r\n')

To get the desired output

line = line.replace(b'\r\r\n', b'')
Wizhi
  • 6,424
  • 4
  • 25
  • 47
tripleee
  • 175,061
  • 34
  • 275
  • 318