小编med*_*ter的帖子

包含额外逗号的csv文件的CSV模块问题

我正在跟随Clinton W. Brownley 撰写的" 基于Python分析基础 "一书(O'Reilly Media Inc.)

对于第2章 - 使用csv模块读取和写入CSV文件(第2部分)基本Python

脚本如下:

#!/usr/bin/env python3
import sys
import csv

input_file = sys.argv[1]
output_file = sys.argv[2]

with open(input_file, 'r', newline='') as csv_input_file:
    with open(output_file, 'w', newline='') as csv_output_file:

        filereader = csv.reader(csv_input_file, delimiter=',')
        filewriter = csv.writer(csv_output_file, delimiter=',')

        for row_list in filereader:
            print(row_list)
            filewriter.writerow(row_list)
Run Code Online (Sandbox Code Playgroud)

输入文件包含逗号(最后两行中的美元金额)字段:

Supplier Name,Invoice Number,Part Number,Cost,Purchase Date
Supplier X,001-1001,2341,$500.00,1/20/14
Supplier X,001-1001,2341,$500.00,1/20/14
Supplier X,001-1001,5467,$750.00,1/20/14
Supplier X,001-1001,5467,$750.00,1/20/14
Supplier Y,50-9501,7009,$250.00,1/30/14
Supplier Y,50-9501,7009,$250.00,1/30/14
Supplier Y,50-9505,6650,$125.00,2/3/14
Supplier Y,50-9505,6650,$125.00,2/3/14
Supplier Z,920-4803,3321,$615.00,2/3/14
Supplier Z,920-4804,3321,$615.00,2/10/14
Supplier Z,920-4805,3321,$6,015.00,2/17/14 …
Run Code Online (Sandbox Code Playgroud)

python csv

1
推荐指数
1
解决办法
635
查看次数

标签 统计

csv ×1

python ×1