Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CSV文件包含意外的双空格Python_Python_Csv - Fatal编程技术网

CSV文件包含意外的双空格Python

CSV文件包含意外的双空格Python,python,csv,Python,Csv,使用Python2.7打开一个文件,将其转换为一个字典,其中第0行作为键,第3行作为值。然后打开文件b,为第0行中的匹配ID号追加匹配的其他列。然后转换回列表并合并两个CSV 最后添加标题并另存为“output.csv” 以及我所期望的例子: 文件a: 1234, 14/12/2,123.4, 5618, 13/1/12,23.4, 9143, 15/2/14,222.4, 文件b: 1234,abc,cda 9143,nda, bad 5618, ede, fpc (d)产出: (实际)

使用Python2.7打开一个文件,将其转换为一个字典,其中第0行作为键,第3行作为值。然后打开文件b,为第0行中的匹配ID号追加匹配的其他列。然后转换回列表并合并两个CSV

最后添加标题并另存为“output.csv”

以及我所期望的例子:

文件a:

1234, 14/12/2,123.4,
5618, 13/1/12,23.4,
9143, 15/2/14,222.4,
文件b:

1234,abc,cda
9143,nda, bad 
5618, ede, fpc
(d)产出:

(实际)产出:

守则:

import csv

#create a dict from first csv, with clearing solution id as key
with open("DDS.csv", "rb") as f:
    first = {rows[0]: rows[3:] for rows in list(csv.reader(f))}

# compare second csv, append rank, add received recurring columns
with open("report.csv", "rb") as f:
    for row in csv.reader(f):
        if row and row[0] in first:  # row[0] = clearing solution id
            first[row[0]].append(row[1])  # row[1] = rank
            first[row[0]].append(row[2])
            first[row[0]].append('Received')
            first[row[0]].append('Recurring')


# convert dict back to list
merged = [(k,) + tuple(v) for k, v in first.items()]

# write list to output csv
with open('output.csv', "w") as f:
    writer = csv.DictWriter(f, fieldnames =['ID', 'Payment Date', 'Payment Amount', 'Other ID','other other ID', 'Payment Status', 'Payment Type'])
    writer.writeheader()
    csv.writer(f).writerows(merged)
奖励积分: 如何从输出CSV中删除第一列


谢谢

您可以去掉第一个,只需不将
(k,)
添加到元组并删除即可 “ID”来自您的字段名。您不需要创建另一个writer来写入行
csv.writer(f).writerows(merged)
?:


我建议使用经典的python字符串操作,而不是csv模块


例如,使用
行.replace(“”,).split(,)[0]
而不是
行[0]
应该可以解决空间问题。

您看到的额外空行的直接问题应该通过在
csv.DictWriter()
中添加
lineterminator='\n'
参数来解决,在Windows上,csv模块默认使用
\r\n
的行终止符,请参阅类似问题,例如“谢谢,这很有效:)
ID, payment date, payment amount, other id, other other id, payment status, payment type
1234, 14/12/2,123.4,1234,abc,cda, Received, Recurring

 5618, 13/1/12,23.4,9143,nda, bad, Received, Recurring 

 9143, 15/2/14,222.4,5618, ede, fpc,Received, Recurring
import csv

#create a dict from first csv, with clearing solution id as key
with open("DDS.csv", "rb") as f:
    first = {rows[0]: rows[3:] for rows in list(csv.reader(f))}

# compare second csv, append rank, add received recurring columns
with open("report.csv", "rb") as f:
    for row in csv.reader(f):
        if row and row[0] in first:  # row[0] = clearing solution id
            first[row[0]].append(row[1])  # row[1] = rank
            first[row[0]].append(row[2])
            first[row[0]].append('Received')
            first[row[0]].append('Recurring')


# convert dict back to list
merged = [(k,) + tuple(v) for k, v in first.items()]

# write list to output csv
with open('output.csv', "w") as f:
    writer = csv.DictWriter(f, fieldnames =['ID', 'Payment Date', 'Payment Amount', 'Other ID','other other ID', 'Payment Status', 'Payment Type'])
    writer.writeheader()
    csv.writer(f).writerows(merged)
merged = [tuple(v) for k, v in first.items()]
with open('output.csv', "w") as f:
    writer = csv.DictWriter(f, fieldnames =['Payment Date', 'Payment Amount', 'Other ID','other other ID', 'Payment Status', 'Payment Type'])
    writer.writeheader()
    writer.writerows(merged)