如何在python中使用for循环逐个读取行
我想使用For循环,并根据需要逐个打印一行。 这是我的密码:如何在python中使用for循环逐个读取行,python,csv,row,Python,Csv,Row,我想使用For循环,并根据需要逐个打印一行。 这是我的密码: import csv with open("details.csv") as csvFile: reader = csv.DictReader(csvFile) for row in reader: if['age'] == '21': print(row['Name'], row['age'], row['DOB']) else: co
import csv
with open("details.csv") as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
if['age'] == '21':
print(row['Name'], row['age'], row['DOB'])
else:
continue
在这里,我希望运行for循环6次,并且我希望获得年龄为“21”的人的具体数据,该人的详细信息仅我希望打印,如果不是“21”,则跳过该行。但是我的代码不是我想要的。
有人能帮我吗。。?
谢谢:)
我的csv是:
Name age dob
Arun 21 01/08/93
Banni 20 05/11/94
charan 23 23/03/92
nani 21 04/05/93
简单错误:试试这个
import csv
with open("details.csv") as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
if row['age'].strip() == '21': #error in this line
print(row['Name'], row['age'], row['DOB'])
else:
continue
简单错误:试试这个
import csv
with open("details.csv") as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
if row['age'].strip() == '21': #error in this line
print(row['Name'], row['age'], row['DOB'])
else:
continue
简单错误:试试这个
import csv
with open("details.csv") as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
if row['age'].strip() == '21': #error in this line
print(row['Name'], row['age'], row['DOB'])
else:
continue
简单错误:试试这个
import csv
with open("details.csv") as csvFile:
reader = csv.DictReader(csvFile)
for row in reader:
if row['age'].strip() == '21': #error in this line
print(row['Name'], row['age'], row['DOB'])
else:
continue
如果没有在csv文件中指定标头,请尝试此操作
import csv
import sys
f = open("File_Name.csv", 'rt') #open the file reading
print "Name\tAge\tDOB"
try:
reader = csv.reader(f)
for row in reader:
if row[1] == '21': #Check the condition
print "%s\t%s\t%s" %(row[0],row[1],row[2]) #print the columns
else:
continue
finally:
f.close() #close the file at the end
如果文件的头作为第一行(名称、年龄、DOB),则使用以下命令
import csv #import csv package
with open("details.csv") as csvFile: #open the file
reader = csv.DictReader(csvFile)
for row in reader: #iterate the file
if row['age'].strip() == '21': #check the condition
print(row['Name'], row['age'], row['DOB'])
else: #skip if not satisfies the condition
continue
导入csv#导入csv包
将open(“details.csv”)作为csvFile:#打开文件
reader=csv.DictReader(csvFile)
对于读取器中的行:#迭代文件
如果行['age'].strip()='21':#检查条件
打印(第['Name']行、第['age']行、第['DOB']行)
否则:#如果不满足条件,则跳过
持续
如果您没有在csv文件中指定头文件,请尝试此操作
import csv
import sys
f = open("File_Name.csv", 'rt') #open the file reading
print "Name\tAge\tDOB"
try:
reader = csv.reader(f)
for row in reader:
if row[1] == '21': #Check the condition
print "%s\t%s\t%s" %(row[0],row[1],row[2]) #print the columns
else:
continue
finally:
f.close() #close the file at the end
如果文件的头作为第一行(名称、年龄、DOB),则使用以下命令
import csv #import csv package
with open("details.csv") as csvFile: #open the file
reader = csv.DictReader(csvFile)
for row in reader: #iterate the file
if row['age'].strip() == '21': #check the condition
print(row['Name'], row['age'], row['DOB'])
else: #skip if not satisfies the condition
continue
导入csv#导入csv包
将open(“details.csv”)作为csvFile:#打开文件
reader=csv.DictReader(csvFile)
对于读取器中的行:#迭代文件
如果行['age'].strip()='21':#检查条件
打印(第['Name']行、第['age']行、第['DOB']行)
否则:#如果不满足条件,则跳过
持续
如果您没有在csv文件中指定头文件,请尝试此操作
import csv
import sys
f = open("File_Name.csv", 'rt') #open the file reading
print "Name\tAge\tDOB"
try:
reader = csv.reader(f)
for row in reader:
if row[1] == '21': #Check the condition
print "%s\t%s\t%s" %(row[0],row[1],row[2]) #print the columns
else:
continue
finally:
f.close() #close the file at the end
如果文件的头作为第一行(名称、年龄、DOB),则使用以下命令
import csv #import csv package
with open("details.csv") as csvFile: #open the file
reader = csv.DictReader(csvFile)
for row in reader: #iterate the file
if row['age'].strip() == '21': #check the condition
print(row['Name'], row['age'], row['DOB'])
else: #skip if not satisfies the condition
continue
导入csv#导入csv包
将open(“details.csv”)作为csvFile:#打开文件
reader=csv.DictReader(csvFile)
对于读取器中的行:#迭代文件
如果行['age'].strip()='21':#检查条件
打印(第['Name']行、第['age']行、第['DOB']行)
否则:#如果不满足条件,则跳过
持续
如果您没有在csv文件中指定头文件,请尝试此操作
import csv
import sys
f = open("File_Name.csv", 'rt') #open the file reading
print "Name\tAge\tDOB"
try:
reader = csv.reader(f)
for row in reader:
if row[1] == '21': #Check the condition
print "%s\t%s\t%s" %(row[0],row[1],row[2]) #print the columns
else:
continue
finally:
f.close() #close the file at the end
如果文件的头作为第一行(名称、年龄、DOB),则使用以下命令
import csv #import csv package
with open("details.csv") as csvFile: #open the file
reader = csv.DictReader(csvFile)
for row in reader: #iterate the file
if row['age'].strip() == '21': #check the condition
print(row['Name'], row['age'], row['DOB'])
else: #skip if not satisfies the condition
continue
导入csv#导入csv包
将open(“details.csv”)作为csvFile:#打开文件
reader=csv.DictReader(csvFile)
对于读取器中的行:#迭代文件
如果行['age'].strip()='21':#检查条件
打印(第['Name']行、第['age']行、第['DOB']行)
否则:#如果不满足条件,则跳过
持续
默认情况下,DictReader使用逗号,
作为两个字段之间的分隔符,但您的csv文件使用制表符
如果您不想更改csv文件,解决方案是将读写器的创建更改为
reader = csv.DictReader(f, delimiter='\t')
下一步将行if['age']=='21':
更改为if行['age']=='21':
最后,行['DOB']
应该是行['DOB']
,因为字段名区分大小写。默认情况下,DictReader
使用逗号,
作为两个字段之间的分隔符,但您的csv文件使用制表符
如果您不想更改csv文件,解决方案是将读写器的创建更改为
reader = csv.DictReader(f, delimiter='\t')
下一步将行if['age']=='21':
更改为if行['age']=='21':
最后,行['DOB']
应该是行['DOB']
,因为字段名区分大小写。默认情况下,DictReader
使用逗号,
作为两个字段之间的分隔符,但您的csv文件使用制表符
如果您不想更改csv文件,解决方案是将读写器的创建更改为
reader = csv.DictReader(f, delimiter='\t')
下一步将行if['age']=='21':
更改为if行['age']=='21':
最后,行['DOB']
应该是行['DOB']
,因为字段名区分大小写。默认情况下,DictReader
使用逗号,
作为两个字段之间的分隔符,但您的csv文件使用制表符
如果您不想更改csv文件,解决方案是将读写器的创建更改为
reader = csv.DictReader(f, delimiter='\t')
下一步将行if['age']=='21':
更改为if行['age']=='21':
最后,行['DOB']
应该是行['DOB']
,因为字段名区分大小写。我尝试了您的代码和文件,但出现了一个错误。
然后我用逗号替换了csv文件中的制表符,并将“dob”大写为“dob”:
然后输出是正确的:
>>>
Arun 21 01/08/93
nani 21 04/05/93
>>>
当然,我将第5行更改为,如果行['age']='21':
我尝试了您的代码和文件,但出现了一个错误。
然后我用逗号替换了csv文件中的制表符,并将“dob”大写为“dob”:
然后输出是正确的:
>>>
Arun 21 01/08/93
nani 21 04/05/93
>>>
当然,我将第5行更改为,如果行['age']='21':
我尝试了您的代码和文件,但出现了一个错误。
然后我用逗号替换了csv文件中的制表符,并将“dob”大写为“dob”:
然后输出是正确的:
>>>
Arun 21 01/08/93
nani 21 04/05/93
>>>
当然,我将第5行更改为,如果行['age']='21':
我尝试了您的代码和文件,但出现了一个错误。
然后我用逗号替换了csv文件中的制表符,并将“dob”大写为“dob”:
然后输出是正确的:
>>>
Arun 21 01/08/93
nani 21 04/05/93
>>>
当然,我将第5行更改为,如果行['age']='21':
那么您的输出是什么?@kasra,它没有打印任何东西。。可能是语法错误我猜是['age']='21'还是第['age']='21'行@duck,我试过了,但都不起作用。你能给我们一个csv内容示例吗?那么你的输出是什么?@kasra,它没有打印任何东西。。可能是语法错误我猜是['age']='21'还是行['age']='21'@duck,我试过了,但都不起作用。你能给我们一个csv内容示例吗