List 使用csv.reader生成while循环打印多个列表
我有一个excel文档,里面填充了我希望python代码打印出来的信息。为了让大家了解我的excel文档的外观,我将打印一些代码:List 使用csv.reader生成while循环打印多个列表,list,python-2.7,csv,while-loop,repeat,List,Python 2.7,Csv,While Loop,Repeat,我有一个excel文档,里面填充了我希望python代码打印出来的信息。为了让大家了解我的excel文档的外观,我将打印一些代码: import csv file = csv.reader(open("n:\population.csv")) print file.next() 这为我们提供了以下输出: ['country', 'country isocode', 'year', 'POP'] 但这不是我要打印的部分,相关信息在其他行中,因此如果我再输入一个打印文件,我们将获得以下信息
import csv
file = csv.reader(open("n:\population.csv"))
print file.next()
这为我们提供了以下输出:
['country', 'country isocode', 'year', 'POP']
但这不是我要打印的部分,相关信息在其他行中,因此如果我再输入一个打印文件,我们将获得以下信息:
['Netherlands', 'NLD', '1950', '10113.527']
如果这是我需要的相关信息,现在我只需要打印出年份和人口流行,并对我文档中的每一行执行此操作。因此,我提出了以下代码来实现这一点:
import csv
file = csv.reader(open("population.csv"))
print file.next()
def print_population_list(filename):
population_list_length = len("population.csv")
number = 0
line = file.next()
while number < population_list_length:
print line[2:]
population_list_length = population_list_length - 1
print_population_list('N:\population.csv')
那么,如何让代码打印出文档的下一行,而不是一次又一次地打印同一行,直到数字>填充列表长度
如果有人能帮我解决这个问题,我会非常感激的。你没有做的事情之一就是在while循环或for循环中前进 您已将行定义为file.next,但随后告诉它从同一行反复打印零件 试着这样做,在打印数据后,记下在每个循环结束时推进行:
def print_population_list(filename):
population_list_length = len("population.csv")
number = 0
line = file.next()
while number < population_list_length:
print line[2:]
# the next line of code advances the line in the file
line = file.next()
population_list_length = population_list_length - 1
# you don't need to advance the line here,
# as you've already advanced it at the end of the last loop
for contry, isocode, year, population in
print line[2:]
# the next line of code advances the line in the file
line = file.next()
population_list_length = population_list_length - 1
def print_population_list(filename):
population_list_length = len("population.csv")
number = 0
line = file.next()
while number < population_list_length:
print line[2:]
# the next line of code advances the line in the file
line = file.next()
population_list_length = population_list_length - 1
# you don't need to advance the line here,
# as you've already advanced it at the end of the last loop
for contry, isocode, year, population in
print line[2:]
# the next line of code advances the line in the file
line = file.next()
population_list_length = population_list_length - 1