Jul-12-2020, 02:23 PM
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
output obtained is
Expected output is
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
1 2 3 4 5 6 7 8 9 10 11 12 |
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:['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