Python CSV读取器,未获得预期输出
下面是代码,打开文件并用python将其传递给CSV处理程序Python CSV读取器,未获得预期输出,python,python-3.x,csv,visual-studio-code,file-handling,Python,Python 3.x,Csv,Visual Studio Code,File Handling,下面是代码,打开文件并用python将其传递给CSV处理程序 def csv_reader_dict(): #Reading Data From CSV file into Dictionary using csv print("---------Reading Data From CSV file into Dictionary using csv------------") with open('input.txt','r') as file_data: csv_form
def csv_reader_dict():
#Reading Data From CSV file into Dictionary using csv
print("---------Reading Data From CSV file into Dictionary using csv------------")
with open('input.txt','r') as file_data:
csv_format = csv.DictReader(file_data)
line_count2 = 0
for row in csv_format:
if line_count2 == 0:
print("Header are {}".format(",".join(row) ))
line_count2 += 1
else:
print("{} reside at {} and joined on {}".format(row["name"],row["address"],row["date joined"]))
line_count2 += 1
print("Processed {} line count".format(line_count2))
print("------------------------------------------------------------------------")
print("\n")
csv_reader_dict()
预期产出:
实际产量:
DictReader假定第一行是标题,因此跳过它。在代码中,您使用的是第一次迭代来打印标题,而不是所需的信息 将
fieldnames
添加到DictReader
应该可以工作,因为它不会跳过第一行,并且您的代码将按预期工作
def csv_reader_dict():
#Reading Data From CSV file into Dictionary using csv
print("---------Reading Data From CSV file into Dictionary using csv------------")
with open('input.txt','r') as file_data:
csv_format = csv.DictReader(file_data, fieldnames=['name', 'address', 'date joined'])
line_count2 = 0
for row in csv_format:
if line_count2 == 0:
print("Header are {}".format(",".join(row) ))
line_count2 += 1
else:
print("{} reside at {} and joined on {}".format(row["name"],row["address"],row["date joined"]))
line_count2 += 1
print("Processed {} line count".format(line_count2))
print("------------------------------------------------------------------------")
print("\n")
DictReader假定第一行是标题,因此跳过它。在代码中,您使用的是第一次迭代来打印标题,而不是所需的信息 将
fieldnames
添加到DictReader
应该可以工作,因为它不会跳过第一行,并且您的代码将按预期工作
def csv_reader_dict():
#Reading Data From CSV file into Dictionary using csv
print("---------Reading Data From CSV file into Dictionary using csv------------")
with open('input.txt','r') as file_data:
csv_format = csv.DictReader(file_data, fieldnames=['name', 'address', 'date joined'])
line_count2 = 0
for row in csv_format:
if line_count2 == 0:
print("Header are {}".format(",".join(row) ))
line_count2 += 1
else:
print("{} reside at {} and joined on {}".format(row["name"],row["address"],row["date joined"]))
line_count2 += 1
print("Processed {} line count".format(line_count2))
print("------------------------------------------------------------------------")
print("\n")
当
line\u count2==0
它在第一行进行迭代(john smith),并且当line\u count2==0
时,您强制打印标题
您认为行print(“Header是{}”.format(“,”.join(row))
正在打印头,但它正在打印第一行的键,它们是相同的头
您可以简化该功能:
def csv_reader_dict():
#使用CSV将数据从CSV文件读入字典
打印(“-----------使用CSV将数据从CSV文件读入字典-----------------”)
以open('input.txt','r')作为文件数据:
csv\u格式=csv.DictReader(文件\u数据)
第2行=0
打印(“标题为{}”.format(“,”.join(csv_format.fieldnames)))
对于csv_格式的行:
打印(“{}位于{}并在{}上联接”。格式(第[“名称”]行、第[“地址”]行、第[“联接日期”]))
行数2+=1
打印(“已处理{}行计数”。格式(行计数2))
打印(“--------------------------------------------------------------------------------------”)
打印(“\n”)
csv_读取器_dict()
当line\u count2==0
它在第一行中迭代(john smith),并且您在line\u count2==0
时强制打印标题
您认为行print(“Header是{}”.format(“,”.join(row))
正在打印头,但它正在打印第一行的键,它们是相同的头
您可以简化该功能:
def csv_reader_dict():
#使用CSV将数据从CSV文件读入字典
打印(“-----------使用CSV将数据从CSV文件读入字典-----------------”)
以open('input.txt','r')作为文件数据:
csv\u格式=csv.DictReader(文件\u数据)
第2行=0
打印(“标题为{}”.format(“,”.join(csv_format.fieldnames)))
对于csv_格式的行:
打印(“{}位于{}并在{}上联接”。格式(第[“名称”]行、第[“地址”]行、第[“联接日期”]))
行数2+=1
打印(“已处理{}行计数”。格式(行计数2))
打印(“--------------------------------------------------------------------------------------”)
打印(“\n”)
csv_读取器_dict()
数据是什么,预期的输出是什么,您得到了什么?添加的输出图像数据的内容是什么?列之间用逗号分隔?地址可能与CSV分隔符冲突。姓名、地址、日期加入john smith,“1132 Anywhere Lane Hoboken NJ,07030”,1月4日erica meyers,“1234 smith Lane Hoboken NJ,07030”,3月2日数据是什么,预期输出是什么,您得到了什么?添加的输出图像数据内容是什么?列之间用逗号分隔?地址可能与CSV分隔符冲突。姓名、地址、加入日期john smith,“1132 Anywhere Lane Hoboken NJ,07030”,1月4日erica meyers,“1234 smith Lane Hoboken NJ,07030”,3月2日