使用Python从CSV删除不需要的逗号 [英] Remove unwanted commas from CSV using Python

查看:620
本文介绍了使用Python从CSV删除不需要的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助,我有一个包含地址字段的CSV文件,无论谁将数据输入原始数据库并用逗号分隔地址的不同部分,例如:

I need some help, I have a CSV file that contains an address field, whoever input the data into the original database used commas to separate different parts of the address - for example:

公园街5号楼

当我尝试使用CSV文件时,实际上将其作为单个字段将其视为两个单独的字段.我已经使用Python去除了逗号之间的逗号,因为很容易将它们与实际上应该存在的逗号区分开来,但是这个问题让我很困惑.

When I try to use the CSV file it treats this one entry as two separate fields when in fact it is a single field. I have used Python to strip commas out where they are between inverted commas as it is easy to distinguish them from a comma that should actually be there, however this problem has me stumped.

任何帮助都会感激不尽.

Any help would be gratefully received.

谢谢.

推荐答案

您可以使用 Python的CSV阅读器.例如:

使用此CSV:

1,`Flat 5, Park Street`

这个Python:

import csv

with open('14144315.csv', 'rb') as csvfile:
    rowreader = csv.reader(csvfile, delimiter=',', quotechar='`')
    for row in rowreader:
        print row

您将看到以下输出:

['1', 'Flat 5, Park Street']

这将使用逗号分隔值,但对于引号逗号则使用反引号

This would use commas to separate values but inverted commas for quoted commas

这篇关于使用Python从CSV删除不需要的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆