简体   繁体   English

如何将列表中的数据写入 CSV 文件?

[英]How do you write data from a list into a CSV file?

I have been given a an original csv file, and tasked with reading it, adding a new column for average and then writing the updated data back out to another csv file.我得到了一个原始的 csv 文件,并负责读取它,添加一个新的平均值列,然后将更新的数据写回另一个 csv 文件。

Everything has been successful up to the writing of the data from the list.直到从列表中写入数据为止,一切都已成功。

"""Student Do: Sales Analysis.

This script will use the Pathlib library to set the file path,
use the csv library to read in the file, and iterate over each
row of the file to calculate customer sales averages.
"""

# @TODO: Import the pathlib and csv library
from pathlib import Path
import csv

# @TODO: Set the file path
csv_path = Path("Resources/sales.csv")

# Initialize list of records
records = []

# @TODO: Open the csv file as an object
with open (csv_path, 'r') as csv_file:

    # @TODO:
    # Pass in the csv file to the csv.reader() function
    # (with ',' as the delmiter/separator) and return the csvreader object
    csv_reader = csv.reader(csv_file, delimiter=',')
    # @TODO: Read the header row
    header = next(csv_reader)
    # @TODO: Print the header


    # @TODO: Append the column 'Average' to the header
    header.append('Average')
    print (header)

    # @TODO: Append the header to the list of records
    records.append(header)

    # @TODO: Read each row of data after the header
    for row in csv_reader:
        # @TODO: Print the row
        #print(row)
        # @TODO:
        # Set the 'name', 'count', 'revenue' variables for better
        # readability, convert strings to ints for numerical calculations
        name = row[0]
        count = int(row[1])
        revenue = int(row[2])
        records.append(name)
        records.append(count)
        records.append(revenue)


        # @TODO: Calculate the average (round to the nearest 2 decimal places)
        average = round(revenue/count, 2)

        # @TODO: Append the average to the row
        row.append(average)
        # @TODO: Append the row to the list of records
        records.append(row[3])
        print (row)

        
# @TODO: Set the path for the output.csv
csv_output_path = Path("output.csv")

# @TODO:
# Open the output path as a file and pass into the 'csv.writer()' function
# Set the delimiter/separater as a ','
with open (csv_output_path, 'w') as file:
    csv_writer = csv.writer(file, delimiter=',')
    # @TODO:
    # Loop through the list of records and write every record to the
    # output csv file
    csv_writer.writerow(header)
    for row in records:
        csv_writer.writerow(records)
        
for data in records:
    print (data)

The output file is all wrong, the header is in the name section, the name is in the count section, the count is in revenue and everything else is in the average section output 文件全错,header 在名称部分,名称在计数部分,计数在收入中,其他一切都在平均部分

replace csv_writer.writerow(records) with csv_writer.writerow(row)用 csv_writer.writerow(row) 替换 csv_writer.writerow(records)

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

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