简体   繁体   English

使用python追加csv文件数据

[英]csv file data appending using python

I've been trying to append certain data from input.csv to output.csv using python.我一直在尝试使用 python 将 input.csv 中的某些数据附加到 output.csv 中。

My code is as under:我的代码如下:


    import csv
    from csv import writer
    from csv import reader
    
    
    csvPath = r'C:\Users\Nitin Kumar\Downloads\annual-enterprise-survey-2020-csv-new.csv'
    csvWrite = r'C:\Users\Nitin Kumar\Downloads\copied.csv'
    
    rslt=[]
    with open(csvPath, encoding='utf-8-sig') as csvfile:
    reader = csv.reader(csvfile)

    count=0
    fsa=[]
    for row in reader:
        count=count+1
        print(row)
        rslt.append(row)
        # if count>20:
        #     break
print(rslt)

with open(csvPath, 'r') as read_obj, \
        open(csvWrite, 'a', newline='') as write_obj:
    csv_reader = csv.reader(read_obj)
    csv_writer = csv.writer(write_obj)
    for row_ in csv_reader:
        row_.append(rslt)
        csv_writer.writerow(row_)

The input csv file is:输入的 csv 文件是:


    Year,Industry_agg,Industry_code,Industry_name,Units,Variable_code,Variable_name,Variable_category,Value,Industry_code_ANZSIC06
    2020,Level 1,99999,All industries,Dollars (millions),H01,Total income,Financial performance,"7,33,258",ANZSIC06 divisions A-S 
    2020,Level 1,99999,All industries,Dollars (millions),H04,Sales,Financial performance,"6,60,630",ANZSIC06 divisions A-S
    2020,Level 1,99999,All industries,Dollars (millions),H05,"Interest, dividend",Financial performance,"54,342",ANZSIC06 divisions A-S 
    2020,Level 1,99999,All industries,Dollars (millions),H07,Non-operating income,Financial performance,"18,285",ANZSIC06 divisions A-S
    2020,Level 1,99999,All industries,Dollars (millions),H08,Total expenditure,Financial performance,"6,54,872",ANZSIC06 divisions A-S

The output file I'm getting is:我得到的输出文件是:

    Name
    Nitin
    Nitin1
    Nitin2
    Nitin3
    Nitin4
    Nitin5
    Year,Industry_agg,Industry_code,Industry_name,Units,Variable_code,Variable_name,Variable_category,Value,Industry_code_ANZSIC06,[]
    2020,Level 1,99999,All industries,Dollars (millions),H01,Total income,Financial performance,"7,33,258",ANZSIC06 divisions A-S ,[]
    2020,Level 1,99999,All industries,Dollars (millions),H04,Sales,Financial performance,"6,60,630",ANZSIC06 divisions A-S,[]
    2020,Level 1,99999,All industries,Dollars (millions),H05,"Interest, dividend",Financial performance,"54,342",ANZSIC06 divisions A-S ,[]
    2020,Level 1,99999,All industries,Dollars (millions),H07,Non-operating income,Financial performance,"18,285",ANZSIC06 divisions A-S,[]
    2020,Level 1,99999,All industries,Dollars (millions),H08,Total expenditure,Financial performance,"6,54,872",ANZSIC06 divisions A-S ,[]


However, the output csv file was having following data previously before appending data:但是,输出 csv 文件在附加数据之前具有以下数据:


    Name
    Nitin
    Nitin1
    Nitin2
    Nitin3
    Nitin4
    Nitin5

I need the output file to be like below:我需要输出文件如下所示:


    Name,Year,Industry_agg,Industry_code,Industry_name,Units,Variable_code,Variable_name,Variable_category,Value,Industry_code_ANZSIC06
    Nitin,2020,Level 1,99999,All industries,Dollars (millions),H01,Total income,Financial performance,"7,33,258",ANZSIC06 divisions A-S 
    Nitin1,2020,Level 1,99999,All industries,Dollars (millions),H04,Sales,Financial performance,"6,60,630",ANZSIC06 divisions A-S
    Nitin2,2020,Level 1,99999,All industries,Dollars (millions),H05,"Interest, dividend",Financial performance,"54,342",ANZSIC06 divisions A-S 
    Nitin3,2020,Level 1,99999,All industries,Dollars (millions),H07,Non-operating income,Financial performance,"18,285",ANZSIC06 divisions A-S
    Nitin4,2020,Level 1,99999,All industries,Dollars (millions),H08,Total expenditure,Financial performance,"6,54,872",ANZSIC06 divisions A-S
    Nitin5

I need my output file to append the input data but not able to get the desired result.我需要我的输出文件来附加输入数据,但无法获得所需的结果。 Previously I was even able to add header in outout csv file.以前我什至可以在 outout csv 文件中添加标题。 Gone through lot of codes already present but none of them helped me in this.经历了很多已经存在的代码,但没有一个对我有帮助。

Combine the lines of the input and output files, and overwrite the output file with the new lines.合并输入和输出文件的行,并用新行覆盖输出文件。

# read input file
with open(input_filename, 'r') as input_file:
    input_lines = input_file.read().split('\n')
    input_lines.append('') # because your input file has 
                           # one less line than your output file
    
# read output file
with open(output_filename, 'r') as output_file:
    output_lines = output_file.read().split()

# combine lines of input and output files
new_lines = [output_line + ',' + input_line for 
             input_line, output_line in zip(input_lines, output_lines)]

# overwrite output file with new lines
with open(output_filename, 'w') as output_file:
    for i, line in enumerate(new_lines):
        end = '\n' if i + 1 < len(new_lines) else '' # make new line except 
                                                     # after last line
        output_file.write(line + end)

You might find it better to use Python's zip_longest() function which is able to easily deal with lists of different lengths.您可能会发现使用 Python 的zip_longest()函数会更好,该函数能够轻松处理不同长度的列表。

Try the following:尝试以下操作:

import csv
from itertools import zip_longest

csvPath = r'C:\Users\Nitin Kumar\Downloads\annual-enterprise-survey-2020-csv-new.csv'
csvWrite = r'C:\Users\Nitin Kumar\Downloads\copied.csv'

# Read all the existing entries
existing = []       # Rows holding: Name, Nitin, Nitin1

with open(csvWrite, encoding='utf-8-sig') as f_existing:
    existing = list(csv.reader(f_existing))

# Read the new entries and combine with the existing entries
with open(csvPath, 'r') as f_input, \
    open(csvWrite, 'w', newline='') as f_output:
    
    csv_input = csv.reader(f_input)
    csv_output = csv.writer(f_output)
    
    for row_existing, row_new in zip_longest(existing, csv_input, fillvalue=''):
        csv_output.writerow([*row_existing, *row_new])

The last line takes all elements from an existing row and combines them with all elements from the new row to create a combined output list row.最后一行从现有行中获取所有元素并将它们与新行中的所有元素组合以创建组合输出列表行。

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

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