Python readline with custom delimiter

hgv picture hgv · Aug 23, 2018 · Viewed 18.2k times · Source

novice here. I am trying to read lines from a file, however a single line in a .txt file has a \n in the middle somewhere and while trying to read that line with .readline python cuts it in the middle and outputs as two lines.

  • when I copy and past the line to this window, it shows up as two lines. So i uploaded the file here: https://ufile.io/npt3n

  • also added screenshot of the file as it shows in txt file.

  • this is group chat history exported from Whatsup..if you are wondering.
  • Please help me to read one line completely as shown in txt file.

.

f= open("f.txt",mode='r',encoding='utf8')

for i in range(4):
    lineText=f.readline()
    print(lineText)

f.close()

enter image description here

Answer

Serge Ballesta picture Serge Ballesta · Aug 23, 2018

Python 3 allows you to define what is the newline for a particular file. It is seldom used, because the default universal newlines mode is very tolerant:

When reading input from the stream, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller.

So here you should made explicit that only '\r\n' is an end of line:

f= open("f.txt",mode='r',encoding='utf8', newline='\r\n')

# use enumerate to show that second line is read as a whole
for i, line in enumerate(fd):   
    print(i, line)