-5

I am trying to read from a tab file from a specific row and I want to process the information line by line in order to create a list with those lines. I read the documentation, but I couldn`t find something useful.

To be more clear I will give an example: Given the file:

id conversation

  1. Hello, How are you
  2. I am fine, thanks for asking
  3. I am glad to hear that

I want to read just conversation row and create the list like this:

list=['Hello,How are you', 'I am fine, thanks for asking', 'I am glad to hear that']
Rahul
  • 10,830
  • 4
  • 53
  • 88
robertg
  • 73
  • 1
  • 8

1 Answers1

0

Here is a an example with a oneliner:

import io

file="""id conversation

1 Hello, How are you

2 I am fine, thanks for asking

3 I am glad to hear that"""

[' '.join(row.strip('\n').split(" ")[1:]) for row in io.StringIO(file).readlines() if row.strip('\n')][1:]

Prints

['Hello, How are you',
 'I am fine, thanks for asking',
 'I am glad to hear that']

import io

file="""id conversation

1 Hello, How are you

2 I am fine, thanks for asking

3 I am glad to hear that"""

with open("test.txt", "w") as f:
    f.write(file)

with open("test.txt") as f:
    mylist = [' '.join(row.strip('\n').split(" ")[1:]) for row in f.readlines() if row.strip('\n')][1:]

mylist
Anton vBR
  • 18,287
  • 5
  • 40
  • 46