【问题标题】:Multiline csv field and Python next(file)多行 csv 字段和 Python 下一个(文件)
【发布时间】:2017-06-21 08:30:36
【问题描述】:

输入文件是标准的 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() 的情况下每行转储文件时,一切都是正确的。但是,我需要跳过第一行(原始标题)。

感谢您的帮助!

【问题讨论】:

  • 使用 csv 模块。

标签: python csv file-io python-3.4


【解决方案1】:

首先,为了读取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)

【讨论】:

  • 谢谢你的回答,但是,这是python3(对不起,应该这么说),所以open的说法是正确的。我也试过next(cut_file, None),没有任何变化。
  • 嗯,好的。我的答案是用 python2.7 测试的。我将为 python3 编辑它。
  • next 而不是skip
  • 抱歉,这里打错了
猜你喜欢
  • 2017-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-27
  • 2017-02-05
  • 2014-11-07
  • 2017-05-08
  • 2020-09-01
相关资源
最近更新 更多