0

I'm using Python to create a list from text extracted from zoom, this is the text...

text= """
09:04:57 РћС‚  John Aiton  РєРѕРјСѓ  Р’СЃРµ : Tomorrow I'm going on a trip GRA
09:05:05 РћС‚  John Aiton  РєРѕРјСѓ  Р’СЃРµ : now I am on a trip GRA
09:08:03 РћС‚  John Aiton  РєРѕРјСѓ  Р’СЃРµ : soon we go down south GRA
"""

When I tried to append to an empty list with this code

my = []
for line in text.splitlines():
    my.append(line[46:])
    print(my)

I got repeated results in the output, the beginning of which is below:

["Tomorrow I'm going on a trip GRA"]
["Tomorrow I'm going on a trip GRA", 'now I am on a trip GRA']
["Tomorrow I'm going on a trip GRA", 'now I am on a trip GRA', 'soon we go down south GRA']
["Tomorrow I'm going on a trip GRA", 'now I am on a trip GRA', 'soon we go down south GRA', 'meet up with a friend LRG']
["Tomorrow I'm going on a trip GRA", 'now I am on a trip GRA', 'soon we go down south GRA', 'meet up with a friend LRG', "it reminds me of a book I'm listening to now LRG"]

How can I avoid this repetition and end up with an output (in list or string form) like this:

Tomorrow I'm going on a trip GRA
now I am on a trip GRA
soon we go down south GRA
1
  • you are doing everything right, just print list at the end not inside for loop Commented Jul 3, 2021 at 7:47

3 Answers 3

1

You can use * unpacking with sep='\n' argument for print:

my = []
for line in text.splitlines():
    my.append(line[46:])
print(*my, sep='\n')

Output:

Tomorrow I'm going on a trip GRA
now I am on a trip GRA
soon we go down south GRA
Sign up to request clarification or add additional context in comments.

Comments

0

Another solution wihtout using the specific index (which can cause some issue) is:

[l.split(' : ')[-1] for l in text.splitlines() if l]

So splitlines() will separate by \n and then for each entry to the list if it's not empty we will split by : and take the last element.

This will result in:

["Tomorrow I'm going on a trip GRA",
 'now I am on a trip GRA',
 'soon we go down south GRA']

Comments

0

There is 2 main ways:

  • print each row during iterating

    my = []
    for line in text.splitlines():
        my.append(line[46:])
        print(my[-1])
    
  • print each after iterating

    for x in my:
        print(x)
    
    // or 
    print(*my, sep='\n')
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.