So I am having trouble understanding how Python creates lists using the .split() string method if I were to give it a file to read.
Here I have a text file with populations from three different countries called population.txt:
United-States 325700000
Canada 37000000
China 13860000000
and in another .py file, I have this code:
populationFile = open("population.txt", 'r')
for populationLine in populationFile:
populationList = populationLine.split()
print(populationList)
populationFile.close()
The output is this:
['China', '13860000000']
Does python essentially put each country and the respective population in separate lists by reading each line as it did with China, or is it by character? Also, how come only one list appears here and not all of them?
Sorry for all the questions, but I will be very grateful for anyone who can help :)
What you are doing is setting the value for populationList on top of the previous iteration. so it is splitting the United States population, then splitting the Canada population and saving it over the United States, then China replaced Canada.
What you can do it append;
populationFile = open("population.txt", 'r')
populationList = [] # create an empty list
for populationLine in populationFile:
populationList.append(populationLine.split()) # append the split string into list
print(populationList)
populationFile.close()
If you would like to optimize this, you can use a with block. It would look like this:
with open("population.txt", 'r') as populationFile:
populationList = [] # create an empty list
for populationLine in populationFile:
populationList.append(populationLine.split())
print(populationList)
This only opens the file temporarily and when the with block is complete, it closes it automatically.
You need to change your code to this
populationFile = open("population.txt", 'r')
temp = None
# create an empty list
populationList = []
for line in populationFile:
# split into different words by the space ' ' character
temp = line.split() # temp = ['Canada', '37000000'] or ['China', '13860000000']
# if spaces exist on either the right or left of any the elements in the temp list
# remove them
temp = [item.strip() for item in temp[:]]
# append temp to the population list
populationList.append(temp)
print(populationList)
populationFile.close()
how come only one list appears here and not all of them?
populationList
is changing after each iteration and losing (by overwriting) its earlier value.
Instead you should try this:
for populationLine in populationFile:
populationList.append(populationLine.split())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.