Python CSV dictReader,没有得到预期的输出
Python CSV dictReader, not getting expected output
下面是代码,打开文件并将其传递给 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_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 假设第一行是 headers 所以它会跳过它。在您的代码中,您正在使用第一次迭代来打印 headers 而不是所需的信息。
将 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_count2 == 0
它遍历第一行 (john smith) 时,你在 line_count2 == 0
时强制打印 header。
您认为 print("Header are {}".format(",".join(row) ))
行打印的是 header,但它打印的是第一行的键,它们是相同的 header。
您可以简化函数:
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
print("Header are {}".format(",".join(csv_format.fieldnames)))
for row in csv_format:
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()
下面是代码,打开文件并将其传递给 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_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 假设第一行是 headers 所以它会跳过它。在您的代码中,您正在使用第一次迭代来打印 headers 而不是所需的信息。
将 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_count2 == 0
它遍历第一行 (john smith) 时,你在 line_count2 == 0
时强制打印 header。
您认为 print("Header are {}".format(",".join(row) ))
行打印的是 header,但它打印的是第一行的键,它们是相同的 header。
您可以简化函数:
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
print("Header are {}".format(",".join(csv_format.fieldnames)))
for row in csv_format:
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()