Python Forum
Detect end of line in text file including line breaks
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Detect end of line in text file including line breaks
#1
Hi folks,

I'm new to Python and trying several little projects to get into the languange.
I have a text file which was exported from a WhatsApp chat group and I try to convert this file line by line to a csv format:

23.11.19, 21:35 - Person A: ndsnldkl odj aso saod saodjd ad?
23.11.19, 21:43 - Person B: nsidd dsidaojd eduq dsajojdipajd adapsd??
23.11.19, 21:44 - Person C: ahush asaosi0sj a0s9uaS SJs !!

The output is a csv format:
23.11.19;21:35;Person A;ndsnldkl odj aso saod saodjd ad?

Thats my (newbie) code:
liste = [] for x in f: date = x[:8] time = x[10:15] pos1 = x.find(":",18,-1) name = x[18:pos1] message = x[pos1+2:-1] liste.append(date) liste.append(time) liste.append(name) liste.append(message)
It works great until I get messages like this with line break:
29.11.19, 15:54 - Person D: ndadad saojd sapods dsap.
ksjd sad aslajd a


The "message = x[pos1+2:-1]" does not recognize the line break.
I had written this code in the past with PHP that works nearly the same, but PHP recognizes the full message including the line breaks.

Any idea how I can fix that?

Greetings,
Daniel
Reply
#2
The problem is with the for x in f which reads the file line by line and not message by message.
Reply
#3
(Dec-18-2019, 09:27 AM)Gribouillis Wrote: The problem is with the for x in f which reads the file line by line and not message by message.

Hi, thanks, is there a better solution for reading the file?
Reply
#4
you need to check if the line starts with pattern dd.mm.yy, hh:mm - .
If it does - it's a new message. if not - it's just a new line in the previous message (i.e. append to previous one).
Don't know what the exported file looks like in case of quotes or if (after a new line) message text starts with the above pattern
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
You can better use regex and there you can include new line/break for message text
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No new line from print in a browser Lou 13 1,113 Dec-01-2025, 06:39 PM
Last Post: noisefloor
  cannot unpack non-iterable int object in urllib3/util/wait.py", line 85, ping_chen_ibm_us 2 893 Aug-01-2025, 02:05 PM
Last Post: ping_chen_ibm_us
  [SOLVED] Why does regex fail cleaning line? Winfried 7 4,854 Jul-11-2025, 11:52 PM
Last Post: Pedroski55
  I am a newbie I like to use the command line Mikel2025 1 1,020 Jun-13-2025, 03:20 PM
Last Post: Gribouillis
  Βad Input on line 12 Azdaghost 5 2,165 Apr-19-2025, 10:22 PM
Last Post: Azdaghost
Question [SOLVED] [Beautiful Soup] Move line to top in HTML head? Winfried 0 946 Apr-13-2025, 05:50 AM
Last Post: Winfried
  Insert command line in script lif 4 2,169 Mar-24-2025, 10:30 PM
Last Post: lif
  Entry field random pull from list, each return own line Bear1981 6 1,849 Feb-25-2025, 06:09 AM
Last Post: Pedroski55
  How to revert back to a previous line from user input Sharkenn64u 2 2,997 Dec-28-2024, 08:02 AM
Last Post: Pedroski55
  Pandas - error when running Pycharm, but works on cmd line zxcv101 2 3,507 Sep-09-2024, 08:03 AM
Last Post: pinkang

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.