简体   繁体   English

如何删除 Csv 文件行之间的一些额外逗号

[英]how to remove some extra commas between lines Csv file

How to remove some extra commas on CSV file sometimes there are 3 or more extra commas, I would like the marked part to become a single column如何去掉CSV文件上的一些多余的逗号有时有3个或更多个多余的逗号,我希望标记的部分成为一个单独的列

correct format is 11 columns, I just want to find the ones that are not and remove the commas正确的格式是 11 列,我只想找到不是的并删除逗号

84,855,648857,8787548,R, mark,one 55, power ,0000081,3434,59190000,defen,six, 84,855,648857,8787548,R, mark,one 55, power ,0000081,3434,59190000,defen,six,

first 5 and last 5 columns are static, only the middle will become a single column and sometimes there are more than 3 extra columns前5列和后5列都是static,只有中间会变成单列,有时会多出3列以上

now i split the 300 GB file to work with python script in loop so there is a folder contain the files现在我拆分 300 GB 文件以循环使用 python 脚本,因此有一个文件夹包含这些文件

the result should be like this结果应该是这样的

84,855,648857,8787548,R,mark one 55 power,0000081,3434,59190000,defen,six,

I suggest reading the csv data into a list, merge them, and write it back:我建议将 csv 数据读取到一个列表中,合并它们,然后写回:

def merge(data):
    result = []
    result += data[:5]
    temporary = ""
    for item in data[5:-5]:
        temporary += item + " "
    result.append(temporary[:-1])
    result += data[-5:]
    return result

This function take a list, start(inclusive), end(exclusive), it merge the range specified and returns the result.这个 function 取一个列表,开始(包括),结束(不包括),它合并指定的范围并返回结果。 For example, calling例如,调用

merge(["84","855","648857","8787548","R","mark","one 55","power","0000081","3434","59190000","defen","six"])

will merge index 5,6,7, and return:将合并索引 5、6、7,并返回:

['84', '855', '648857', '8787548', 'R', 'mark one 55 power', '0000081', '3434', '59190000', 'defen', 'six']

You can then write the list back into a csv.然后您可以将该列表写回 csv。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM