多行csv字段和Python下一个(文件)

时间:2017-06-21 08:30:37

标签: python csv file-io python-3.4

输入文件是标准的CSV文件,大约有200行和70列。

我需要根据架构重命名标头,将其写入新文件并附加原始文件中的数据。

但是,某些字段包含多行文字,当我使用next(file)省略原始标题时,单个字段中的这些行将被解释为新的CSV行(请参阅下面的内容)

原始文件示例:

+----------------+-----------+----------+-----------+------------------+-----------+
|    Summary     | Issue key | Issue id | Parent id |    Issue Type    |  Status   |
+----------------+-----------+----------+-----------+------------------+-----------+
| Project info 2 | ABCDE     |   326974 | NA        | Approval Request | Completed |
| info continues |           |          |           |                  |           |
| in signle cell |           |          |           |                  |           |
+----------------+-----------+----------+-----------+------------------+-----------+

输出后的文件:

+================+===========+=========+===========+==================+===========+
|    Summary     | Issue key | Renamed | Parent id |     Renamed      |  Status   |
+================+===========+=========+===========+==================+===========+
| Project info 2 | ABCDE     |  326974 | NA        | Approval Request | Completed |
+----------------+-----------+---------+-----------+------------------+-----------+
| info continues |           |         |           |                  |           |
+----------------+-----------+---------+-----------+------------------+-----------+
| in single cell |           |         |           |                  |           |
+----------------+-----------+---------+-----------+------------------+-----------+

最后,这是代码:

with open(self.paths['raw_file_path'], "r", encoding='UTF-8', errors="ignore") as cut_file:
                header = cut_file.readline().strip()
                # header renaming code, works well
                with open(self.paths['head_file_path'], "w", encoding='UTF-8',) as head_file:
                        head_file.write(",".join(header))
                        next(cut_file)
                        for line in cut_file:
                            head_file.write(line)

当我在没有next()的情况下每行转储文件时,一切都是正确的。但是,我需要跳过第一行(原始标题)。

感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

首先,为了读取csv文件,最好使用csv模块。

要跳过第一行,您需要使用next(cut_file, None)

以下是使用csv模块轻松重命名标头的解决方案。 感谢the following link

中的答案
import csv
import os

inputFileName = "test.csv"
outputFileName = "new_file.csv"
separator=','
new_header = next(open('header_file.csv')).split(separator)

with open(inputFileName, 'r', encoding='UTF-8') as inFile, open(outputFileName, 'w', encoding='UTF-8') as outfile:
    r = csv.reader(inFile)
    w = csv.writer(outfile)

    next(r, None)  # skip the first row from the reader, the old header
    # write new header
    w.writerow(new_header)

    # copy the rest
    for row in r:
        w.writerow(row)