简体   繁体   English

如何使用if语句将数据写入csv列表

[英]How to write data to a csv list with if statements

I am trying to write data to a csv to a new field/column with if statements (based on values already present in the list). 我正在尝试使用if语句将数据写入csv到新字段/列(基于列表中已存在的值)。 I have a list of UIDs (Field name Class): ACRW, AOC, IFSE, LN, RLW, etc, 我有一个UID列表(字段名称类):ACRW,AOC,IFSE,LN,RLW等,

This is what I have so far: 这是我到目前为止:

import csv
with open('PythonProject.csv', 'r') as rd_list:
    csv_reader = csv.reader(rd_list)
    with open ('new_rdlist.csv', 'w', newline='') as new_csvfile:
        fieldnames = ['Class', 'Description', 'Flex']
        csv_writer = csv.DictWriter (new_csvfile, fieldnames=fieldnames)
        csv_writer.writeheader()
        csv_writer = csv.writer(new_csvfile)
        Class = ''
        for row in csv_reader:
            if Class == 'C':
                value = 'Cable'
            elif Class == 'W':
                value = 'Wave'
            elif Class == 'RT':
                value = 'Therm'
            elif Class == 'H':
                value = 'Heat'
            else:
                value = 'Unit'
            csv_writer.writerow(row)            
input('\nNew List is in your directory, Press Enter to exit')

Expected result is to have the csv populate to be, Class, Description, Flex (which is the new field) (ACRW, unique name, Unit) (AOC, unique name, Unit) (C, unique name, Cable) (IFSE, unique name, Unit) (LN, unique name, Unit) (RLW, unique name, Unit) (W, unique name, Wave) etc... 预期的结果是让csv填充为,Class,Description,Flex(这是新字段)(ACRW,唯一名称,单位)(AOC,唯一名称,单位)(C,唯一名称,电缆)(IFSE,唯一名称,单位)(LN,唯一名称,单位)(RLW,唯一名称,单位)(W,唯一名称,波浪)等...

I hope I understood you right, I assume you intend to read in the "PythonProject.csv" and write the same data with an additional column to "new_rdlist.csv" . 我希望我理解你的权利,我假设你打算在读"PythonProject.csv"有一个额外的列和写入相同的数据"new_rdlist.csv"

Accessing & Manipulating the data 访问和操作数据

The main issue seems to be understanding rows in DictReader and DictWriter . 主要问题似乎是了解DictReaderDictWriter行。 As you are already using DictWriter I suggest using the DictReader class as well. 由于您已经在使用DictWriter我建议也使用DictReader类。 The cells/fields in each row can be accessed by their column names. 可以通过列名访问每行中的单元格/字段。 You can access the "Class" value that way, so the first check would read: 您可以通过这种方式访问​​“Class”值,因此第一次检查将显示为:

if row["Class"] == "C":
  value = "Cable"

you can even directly manipulate the row dictionary: 你甚至可以直接操作行字典:

if row["Class"] == "C":
  row["Flex"] = "Cable"

After that modification the rest should be easy to solve! 在修改之后,其余的应该很容易解决!

Other issues 其他事宜

csv_writer = csv.writer(new_csvfile) is redundant as it overwrites the csv_writer variable again csv_writer = csv.writer(new_csvfile)是多余的,因为它再次覆盖csv_writer变量

(Almost) final code (差不多)最终代码

import csv
with open('PythonProject.csv', 'r') as rd_list:
    csv_reader = csv.DictReader(rd_list)
    with open ('new_rdlist.csv', 'w', newline='') as new_csvfile:
        fieldnames = ['Class', 'Description', 'Flex']
        csv_writer = csv.DictWriter (new_csvfile, fieldnames=fieldnames)
        csv_writer.writeheader()
        for row in csv_reader:
            if row["Class"] == "C":
                row["Flex"] = "Cable"
            ...
                row["Flex"] = "Unit"
            csv_writer.writerow(row)            
input('\nNew List is in your directory, Press Enter to exit')

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

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