简体   繁体   English

Python CSV dictReader,没有得到预期 output

[英]Python CSV dictReader, not getting expected output

Below is the code, opening the file and passing it to CSV handler in python下面是代码,打开文件并将其传递给 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()

Expected Output:预期 Output:

预期产出

Actual Output:实际 Output:

实际输出

DictReader assumes the first row to be headers so it skips it. DictReader 假定第一行是标题,因此它会跳过它。 In your code you are using your first iteration to print headers and not the required information.在您的代码中,您正在使用第一次迭代来打印标题而不是所需的信息。

Adding fieldnames to DictReader should work because it will not skip the first row and your code will work as intended.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")

When line_count2 == 0 it's iterating through the first row (john smith), and you are forcing when line_count2 == 0 to print the header.line_count2 == 0时,它会遍历第一行(john smith),而当line_count2 == 0时,您会强制打印 header。

You think that the line print("Header are {}".format(",".join(row) )) it's printing the header, but it's printing the keys of the first row, that are the same headers.您认为行print("Header are {}".format(",".join(row) ))它正在打印 header,但它正在打印第一行的键,它们是相同的标题。

You can simplify the function:您可以简化 function:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM