extracting data line by line from a text file and storing it in a list in python

Viewed 1944

I have a text file which consists of names of different countries as follows:

enter image description here

I have to extract all these names and store them inside a list using python.

Python Code:

with open('a.txt') as x:
    b = [word for line in x for word in line.split()]
    print(b)

Problem: The above python codes works absolutely fine but the only problem is that if it finds any space between any 2 words, it is storing them as two separate words in a list. Whereas, I want to retrieve the names line by line and store that entire word as a single word.

For Example: I want to store the word Antigua & Deps as a single word inside the list. Whereas, it is storing it as 3 different words.

Can anyone please help me out with this problem?

3 Answers

You can directly use readlines on your file handler:

with open('a.txt') as x:
    b = x.readlines()

This will have trailing newline character at the end of each line which you can avoid by:

with open('a.txt') as x:
    b = [line.strip() for line in x]

Why do you get your output?

You are doing for word in line.split() which infact is splitting on whitespaces in each line.

you can just keep the line:

b = [line.strip() for line in x]

If you want to read line by line and you want control some thing then you can do this code

List = [] 
f = open("countries.txt", "r")
for x in f:
  List.append(x.strip())

f.close()

print(List)
Related