简体   繁体   English

Python字典转CSV问题

[英]Python Dictionary to CSV Issue

I put together a python script to clean CSV files. 我整理了一个python脚本来清理CSV文件。 The reformatting works, but the data rows the writer writes to the new CSV file are wrong. 重新格式化有效,但是编写器写入新CSV文件的数据行错误。 I am constructing a dictionary of all rows of data before writing using writer.writerows(). 在使用writer.writerows()进行写入之前,我正在构造所有数据行的字典。 When I check the dictionary using print statements, the correct data is appending to the list. 当我使用打印语句检查字典时,正确的数据将追加到列表中。 However, after appending, the incorrect values are in the dictionary. 但是,追加后,不正确的值在词典中。

import csv

data = []
with open(r'C:\\Data\\input.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    street_fields = []                                  # Store new field names in list
    street_fields.append("startdate")
    street_fields.append("starttime")
    street_fields.append("sitecode")
    street_fields.append("recordtime")
    street_fields.append("direction")
    street_fields.append("turnright")
    street_fields.append("wentthrough")
    street_fields.append("turnleft")
    street_fields.append("pedestrians")
    for row in csv_reader:                              # Read input rows
        if line_count == 0:
            startdate = row[1]                          # Get Start Date from B1
            line_count += 1
        elif line_count == 1:
            starttime = row[1]                          # Get Start Time from B2
            line_count += 1
        elif line_count == 2:
            sitecode = str(row[1])                      # Get Site code from B3
            line_count += 1
        elif line_count == 3:
            street_count = len(row) - 3                 # Determine number of streets in report
            streetnames = []
            i = 1
            while i < street_count:
                streetnames.append(row[i])              # Add streets to list
                i += 4
            line_count += 1
        elif line_count > 4:
            street_values = {}                          # Create dictionary to store new row values
            n = 1
            for street in streetnames:
                turnright = 0 + n                                       
                wentthrough = 1 + n
                turnleft = 2 + n
                pedestrians = 3 + n
                street_values["startdate"] = startdate
                street_values["starttime"] = starttime
                street_values["sitecode"] = sitecode
                street_values["recordtime"] = row[0]
                street_values["direction"] =  street
                street_values["turnright"] = int(row[turnright])
                street_values["wentthrough"] = int(row[wentthrough])
                street_values["turnleft"] = int(row[turnleft])
                street_values["pedestrians"] = int(row[pedestrians])
                data.append(street_values)                                  # Append row dictionary to list
                #print(street_values)                                       ### UNCOMMENT TO SEE CORRECT ROW DATA ###
                #print(data)                                                ### UNCOMMENT TO SEE INCORRECT ROW DATA ###
                n += 4
            line_count += 1
        else:
            line_count += 1
with open(r'C:\\Data\\output.csv', 'w', newline='', encoding="utf-8") as w_scv_file:
    writer = csv.DictWriter(w_scv_file,fieldnames=street_fields)
    writer.writerow(dict((fn,fn) for fn in street_fields))                  # Write headers to new CSV
    writer.writerows(data)                                                  # Write data from list of dictionaries

An example of the list of dictionaries created (JSON): 创建的词典列表(JSON)的示例:

[  
   {  
      "startdate":"11/9/2017",
      "starttime":"7:00",
      "sitecode":"012345",
      "recordtime":"7:00",
      "direction":"Cloud Dr. From North",
      "turnright":0,
      "wentthrough":2,
      "turnleft":11,
      "pedestrians":0
   },
   {  
      "startdate":"11/9/2017",
      "starttime":"7:00",
      "sitecode":"012345",
      "recordtime":"7:00",
      "direction":"Florida Blvd. From East",
      "turnright":4,
      "wentthrough":433,
      "turnleft":15,
      "pedestrians":0
   },
   {  
      "startdate":"11/9/2017",
      "starttime":"7:00",
      "sitecode":"012345",
      "recordtime":"7:00",
      "direction":"Cloud Dr. From South",
      "turnright":15,
      "wentthrough":4,
      "turnleft":6,
      "pedestrians":0
   },
   {  
      "startdate":"11/9/2017",
      "starttime":"7:00",
      "sitecode":"012345",
      "recordtime":"7:00",
      "direction":"Florida Blvd. From West",
      "turnright":2,
      "wentthrough":219,
      "turnleft":2,
      "pedestrians":0
   },
   {  
      "startdate":"11/9/2017",
      "starttime":"7:00",
      "sitecode":"012345",
      "recordtime":"7:15",
      "direction":"Cloud Dr. From North",
      "turnright":1,
      "wentthrough":3,
      "turnleft":8,
      "pedestrians":0
   }
]

What actually writes to the CSV: 实际写入CSV的内容: 在此处输入图片说明

Note the Direction field and data rows are incorrect. 请注意方向字段和数据行不正确。 For some reason when it loops through the streetnames list, the last street name and the corresponding row values persist for the individual record time. 由于某种原因,当它遍历街道名称列表时,最后的街道名称和相应的行值会在各个记录时间内保持不变。

Do I need to delete my variables before re-assigning them values? 在重新分配变量值之前是否需要删除变量?

It looks like you are appending the same dictionary to the list over and over. 您似乎一遍又一遍地将相同的词典添加到列表中。

In general, when appending a nuber of separate dictionaries to a list, I would use mylist.append(mydict.copy()) , otherwise later on when you assign new values within a dictionary of the same name you are really just updating your old dictionary, including entries in your list that point to a dictionary of the same name (see mutable vs immutable objects in python ). 通常,在将多个单独的字典添加到列表时,我将使用mylist.append(mydict.copy()) ,否则以后在同名字典中分配新值时,实际上只是在更新旧的字典,包括列表中指向同名字典的条目(请参阅python中的可变对象与不可变对象 )。

In short: If you want the dictionary in the list to be a separate entity from the new one, create a deep copy using dict.copy() when appending it to the list. 简而言之:如果您希望列表中的字典与新字典分开,则在将字典追加到列表时使用dict.copy()创建深拷贝。

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

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