正在尝试合并CSV中的三列,正在更新原始CSV

2024-10-06 10:27:11 发布

您现在位置:Python中文网/ 问答频道 /正文

一些示例数据:

title1|title2|title3|title4|merge
test|data|here|and
test|data|343|AND
",3|data|343|and

我的编码尝试:

^{pr2}$

输出应

title1|title2|title3|title4|merge
test|data|here|and|testhereand
test|data|343|AND|test343AND
",3|data|343|and|",3343and

作为您在运行此代码时的参考,第一次打印它将打印我希望在输出csv中出现的行。但是,第二次打印将打印标题行x次,其中x是行数。在

任何输入或更正或工作代码将不胜感激。在


Tags: and数据代码test示例编码datahere
3条回答

最后一行的双引号肯定把csv.DictReader(). 这是有效的:

new_lines = []
with open('file.csv', 'rb') as f:
    # skip the first line
    new_lines.append(f.next().strip())
    for line in f:
        # the newline and split the fields
        line = line.strip().split('|')
        # exctract the field data you want
        title1, title3, title4 = line[0], line[2], line[3]
        # turn the field data into a string and append in to the rest
        line.append(''.join([title1, title3, title4]))
        # save the new line for later
        new_lines.append('|'.join(line))

with open('file.csv', 'w') as f:
    # make one long string and write it to the new file
    f.write('\n'.join(new_lines))
import csv
import StringIO

stored_output = StringIO.StringIO()

with open('file.csv', 'rb') as input_csv:
    reader = csv.DictReader(input_csv, delimiter='|', quoting=csv.QUOTE_NONE)
    writer = csv.DictWriter(stored_output, reader.fieldnames, delimiter="|",quoting=csv.QUOTE_NONE, quotechar=None)

    merge_cols = "title1", "title3", "title4"

    writer.writeheader()

    for row in reader:
        row["merge"] = ''.join(row[col] for col in merge_cols)
        writer.writerow(row)

    contents = stored_output.getvalue()
    stored_output.close()
    print contents

with open('file.csv', 'rb') as input_csv:
    input_csv = input_csv.read().strip()

if input_csv != contents.strip():
    with open('file.csv', 'wb') as new_csv:
        new_csv.write("".join(contents))

我想我们可以把这件事简化。我承认,处理流氓"有点麻烦,因为你必须努力告诉Python你不想为此担心。在

import csv

with open('file.csv', 'rb') as input_csv, open("new_file.csv", "wb") as output_csv:
    reader = csv.DictReader(input_csv, delimiter='|', quoting=csv.QUOTE_NONE)
    writer = csv.DictWriter(output_csv, reader.fieldnames, delimiter="|",quoting=csv.QUOTE_NONE, quotechar=None)

    merge_cols = "title1", "title3", "title4"

    writer.writeheader()

    for row in reader:
        row["merge"] = ''.join(row[col] for col in merge_cols)
        writer.writerow(row)

生产

^{pr2}$

注意,即使你想更新原始文件,我还是拒绝了。为什么?这是个坏主意,因为这样你就可以在处理数据的同时销毁数据。在

我怎么能这么肯定?因为这正是我第一次运行你的代码时所做的,我知道的更多。;^)

相关问题 更多 >