多行 csv 字段和 Python next(file)
Multiline csv field and Python next(file)
输入文件是标准 CSV 文件,大约有 200 行和 70 列。
我需要根据架构重命名 header,将其写入新文件并从原始文件追加数据。
但是,一些字段包含 multi-line 文本,当我使用 next(file)
省略原始 header 时,单个字段中的这些行被解释为新的 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()
的情况下每行转储文件时,一切都是正确的。但是,我需要跳过第一行(原始header)。
感谢您的帮助!
首先,为了读取csv文件,最好使用csv
模块。
为了跳过第一行,您需要使用 next(cut_file, None)
这是使用 csv
模块轻松重命名 header 的解决方案。
感谢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)
输入文件是标准 CSV 文件,大约有 200 行和 70 列。
我需要根据架构重命名 header,将其写入新文件并从原始文件追加数据。
但是,一些字段包含 multi-line 文本,当我使用 next(file)
省略原始 header 时,单个字段中的这些行被解释为新的 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()
的情况下每行转储文件时,一切都是正确的。但是,我需要跳过第一行(原始header)。
感谢您的帮助!
首先,为了读取csv文件,最好使用csv
模块。
为了跳过第一行,您需要使用 next(cut_file, None)
这是使用 csv
模块轻松重命名 header 的解决方案。
感谢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)