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


Messages In This Thread
Collect lines in a file - by sridhar - Jul-12-2020, 02:23 PM
RE: Collect lines in a file - by mlieqo - Jul-12-2020, 03:08 PM
RE: Collect lines in a file - by j.crater - Jul-12-2020, 03:10 PM
RE: Collect lines in a file - by sridhar - Jul-12-2020, 04:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete multiple lines from txt file Lky 6 2,346 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  failing to print not matched lines from second file tester_V 14 6,188 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,102 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Importing a function from another file runs the old lines also dedesssse 6 2,610 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  [Solved] Trying to read specific lines from a file Laplace12 7 3,587 Jun-21-2021, 11:15 AM
Last Post: Laplace12
  all i want to do is count the lines in each file Skaperen 13 4,945 May-23-2021, 11:24 PM
Last Post: Skaperen
  python tool to collect the time measurement of entire code maiya 3 2,333 Feb-12-2021, 05:39 PM
Last Post: BashBedlam
  Split Characters As Lines in File quest_ 3 2,546 Dec-28-2020, 09:31 AM
Last Post: quest_
  Find lines from one file in another tester_V 8 3,442 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 5,977 Aug-10-2020, 11:01 PM
Last Post: medatib531

Forum Jump:

User Panel Messages

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