Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Collect lines in a file
#1
Hi all,
If a line doesn't start with a timestamp , i need to add it with previous line through list.
this is the content of my file
9/16/16, 01:38 - User1: Hi , How you doing
9/16/16, 01:39 - User2: hi,
I'm good
How about you ?
Thanks for asking man
<media>
9/16/16, 02:02 - User3: Howdy folks!
9/16/16, 02:29 - User2: Awesome
9/16/16, 02:29 - User2: we are all good,
Thanks for asking
awesome
7/11/20, 13:00 - Me: <Video> watch this


Here is the code that I designed

intermediate =[]
finalData  = []
file = open('sample.txt', 'r' , encoding="utf8")
for lines in file:
    if startsWithDate(lines):
        intermediate .clear()
        intermediate .append(lines)
    else:
        intermediate .append(lines)
    intr = ' '.join(intermediate )
    finalData.append(intr)
print(finalData)
output obtained is
Output:
['9/16/16, 01:38 - User1: Hi , How you doing\n', '9/16/16, 01:39 - User2: hi,\n', "9/16/16, 01:39 - User2: hi,\n I'm good\n", "9/16/16, 01:39 - User2: hi,\n I'm good\n How about you ?\n", "9/16/16, 01:39 - User2: hi,\n I'm good\n How about you ?\n Thanks for asking man\n", "9/16/16, 01:39 - User2: hi,\n I'm good\n How about you ?\n Thanks for asking man\n <media>\n", '9/16/16, 02:02 - User3: Howdy folks!\n', '9/16/16, 02:29 - User2: Awesome \n', '9/16/16, 02:29 - User2: we are all good,\n', '9/16/16, 02:29 - User2: we are all good,\n Thanks for asking\n', '9/16/16, 02:29 - User2: we are all good,\n Thanks for asking\n awesome\n', '7/11/20, 13:00 - Me: <Video> watch this']
I don't need intermediate repeated outputs which are highlighted above.
Expected output is
Output:
['9/16/16, 01:38 - User1: Hi , How you doing\n', "9/16/16, 01:39 - User2: hi,\n I'm good\n How about you ?\n Thanks for asking man\n <media>\n", '9/16/16, 02:02 - User3: Howdy folks!\n', '9/16/16, 02:29 - User2: Awesome \n', '9/16/16, 02:29 - User2: we are all good,\n Thanks for asking\n awesome\n', '7/11/20, 13:00 - Me: <Video> watch this']
Please help me out
Reply
#2
Collect all messages in intermediate and append to final data only when there is a timestamp (and there is data in intermediate)
intermediate = []
finalData = []
file = open('sample.txt', 'r', encoding="utf8")
for lines in file:
    if startsWithDate(lines) and intermediate:
        finalData.append(' '.join(intermediate))
        intermediate.clear()
    intermediate.append(lines)
Reply
#3
Hello,
the problem is placing of line 11 (appending to finalData). This line happens in each iteration, regardless of whether line started with a date or not.
Apart from that, I would recommend you not to use "file" as a variable name, since it is a Python reserved keyword.
And when working with files, it is recommended to use "with" (context manager). You can read more here:
https://docs.python.org/3/tutorial/input...ting-files
Reply
#4
Thanks! @mlieqo That really helps but I could not able to capture last item upon using your method.
Thanks! @j.crater. I didn't noticed.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  failing to print not matched lines from second file tester_V 13 783 Apr-13-2021, 03:36 PM
Last Post: DeaD_EyE
  python tool to collect the time measurement of entire code maiya 3 335 Feb-12-2021, 05:39 PM
Last Post: BashBedlam
  Split Characters As Lines in File quest_ 3 448 Dec-28-2020, 09:31 AM
Last Post: quest_
  Find lines from one file in another tester_V 8 647 Nov-15-2020, 03:29 AM
Last Post: tester_V
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 1,144 Aug-10-2020, 11:01 PM
Last Post: medatib531
  HELP: collect path name mart79 1 482 May-14-2020, 02:42 PM
Last Post: anbu23
  Can't seem to figure out how to delete several lines from a text file Cosmosso 9 1,246 Dec-10-2019, 11:09 PM
Last Post: Cosmosso
  using vars from one file to match lines in another gt76_noobster 3 971 Jan-30-2019, 05:34 PM
Last Post: ichabod801
  grep command based on lines in a pattern file Skaperen 1 850 Jan-07-2019, 10:23 AM
Last Post: Gribouillis
  Collect real time data jeuvrey 0 1,083 Dec-10-2018, 11:05 AM
Last Post: jeuvrey

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020