简体   繁体   English

如何将一个CSV文件中的多行和一列复制到另一个CSV Excel中?

[英]How to copy multiple rows and one column from one CSV file to another CSV Excel?

I am extremely new to python(coding, for that matter). 我对python(编码)非常陌生。 Could I please get some help as to how can I achieve this. 请问我如何获得帮助。 I have gone through numerous threads but nothing helped. 我经历了无数次讨论,但无济于事。

My input file looks like this: 我的输入文件如下所示:

在此处输入图片说明

I want my output file to look like this: 我希望我的输出文件如下所示:

在此处输入图片说明

Just replication of the first column, twice in the second excel sheet. 只需复制第一列,在第二张excel工作表中复制两次。 With a line after every 5 rows. 每5行后加一行。

A .csv file can be opened with a normal text editor, do this and you'll see that the entries for each column are comma-separated (csv = comma separated values). 可以使用常规文本编辑器打开.csv文件,执行此操作,您将看到每列的条目均以逗号分隔(csv =逗号分隔值)。 Most likely it's semicolons ; 最有可能是分号; , though. 不过。

Since you're new to coding, I recommend trying it manually with a text editor first until you have the desired output, and then try to replicate it with python. 由于您不熟悉编码,因此建议您首先使用文本编辑器手动尝试它,直到获得所需的输出,然后再尝试使用python复制它。

Also, you should post code examples here and ask specific questions about why it doesn't work like you expected it to work. 另外,您应该在此处发布代码示例,并询问有关其为何无法正常运行的特定问题。

Below is the solution. 下面是解决方案。 Don't forget to configure input/output files and the delimiter: 不要忘记配置输入/输出文件和定界符:

input_file = 'c:\Temp\input.csv'
output_file = 'c:\Temp\output.csv'
delimiter = ';'

i = 0
output_data = ''
with open(input_file) as f:
    for line in f:
        i += 1
        output_data += line.strip() + delimiter + line
        if i == 5:
            output_data += '\n'
            i = 0

with open(output_file, 'w') as file_:
    file_.write(output_data)

Python has a csv module for doing this. Python有一个用于执行此操作的csv模块。 It is able to automatically read each row into a list of columns. 它能够自动将每一行读入列列表。 It is then possible to simply take the first element and replicate it into the second column in an output file. 然后可以简单地获取第一个元素并将其复制到输出文件的第二列中。

import csv

with open('input.csv', 'rb') as f_input:
    csv_input = csv.reader(f_input)
    input_rows = list(csv_input)

with open('output.csv', 'wb') as f_output:
    csv_output = csv.writer(f_output)

    for line, row in enumerate(input_rows, start=1):
        csv_output.writerow([row[0], row[0]])

        if line % 5 == 0:
            csv_output.writerow([])

Note, it is not advisable to write the updated data directly over the input file as if there was a problem you would lose your original file. 请注意,建议不要将更新后的数据直接写在输入文件上,因为如果出现问题,您可能会丢失原始文件。

If your input file has multiple columns, this script will remove them and simple duplicate the first column. 如果您的输入文件有多列,此脚本将删除它们并简单地复制第一列。

By default, the csv format separates each column using a comma, this can be modified by specifying a desired delimiter as follows: 默认情况下, csv格式使用逗号分隔每一列,可以通过指定所需的分隔符来修改此格式,如下所示:

csv_output = csv.writer(f_output, delimiter=';')

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

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