如何从Python数据中删除多余的逗号 [英] How to remove extra commas from data in Python

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

问题描述

我有一个CSV文件,试图通过该文件将数据加载到包含2列的SQL表中。我有2列,数据用逗号分隔,标识下一个字段。第二列包含文本和该文本中的一些逗号。
由于存在多余的逗号,因此无法将数据加载到SQL表中,因为它看起来好像有多余的列。我有数百万行数据。如何删除这些多余的逗号?

I have a CSV file through which I am trying to load data into my SQL table containing 2 columns. I have 2 columns and the data is separated by commas, which identify the next field. The second column contains text and some commas in that text. Because of the extra commas I am not able to load data into my SQL table as it looks like it has extra columns. I have millions of rows of data. How can I remove these extra commas?

数据:

Number Address
"12345" , "123 abc street, Unit 345"
"67893" , "567 xyz lane"
"65432" , "789 unit, mno street"

我想删除随机行中地址中的多余逗号。

I would like to remove the extra commas in the addresses in random rows.

推荐答案

如果所有数据的格式都相同,例如数字地址 000, 000 abc street,单位000 ,则可以拆分列表,删除逗号,然后将列表放回一起,再次使其成为字符串。例如,使用您提供的数据:

If all your data will be in the same format, as Number Address "000" , "000 abc street, Unit 000", you can split the list, remove the comma, and put the list back together, making it a string again. For example using the data you gave:

ori_addr = "Number Address \"12345\" , \"123 abc street, Unit 345\""
addr = ori_addr.split()
addr[6] = addr[6].replace(",", "")
together_addr = " ".join(addr)

together_addr等于 Number Address 12345, 123 abc street Unit 345请注意,街道和单位之间没有逗号。

together_addr is equal to "Number Address "12345" , "123 abc street Unit 345" note that there is no comma between "street" and "Unit."

这篇关于如何从Python数据中删除多余的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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