gpt4 book ai didi

python - 如何从一行之间删除换行符而不从行尾删除换行符python?

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:18 26 4
gpt4 key购买 nike

我的输入是一个大的 csv 文件,其中包含如下行:

"7807371008","Sat Jan 16 00:07:46 +0000 2010","@bigg_robb welcome to the party life of politics","T 33.417474,-86.705343","al","23845121","1381","502","Wed Mar 11 22:38:27 +0000 2009","2468"

我想要的输出是一个新文件,其中第一列和第三列仅删除了所有特殊字符:

7807371008,  bigg robb welcome to the party life of politics

但是在文本之间有一些换行符,即使从技术上讲它不是该行的结尾。在这种情况下,我收到错误:

IndexError: list index out of range

此类行的示例是:

"7807376607","Sat Jan 16 00:07:57 +0000 2010","RT @CBS8News:The commander of Gov. Riley's task
force on illegal gambling resigns after winning $2,300 at a MS casino.
gt;#conflictofinterest","Montgomery, Alabama","al","33358058","84","164","Mon Apr 20 00:48:37 +0000 2009","4509"

我的代码是:

import csv
import sys
import re

with open('al.csv') as f:
for line in f:

j = next(csv.reader([line]))
id1 = j[0]
id2 = re.sub('[^A-Za-z0-9\.]+',' ',id1)
tt1 = j[2]
tt2 = re.sub('[^A-Za-z0-9\.]+',' ',tt1)
print id2.strip()+", "+tt2.lower()

我该如何解决这个问题?请帮忙。

最佳答案

您应该将逗号 , 指定为您的 csv 文件分隔符(或基于您的文件的正确分隔符),而且 csv 阅读器对象没有您循环需要访问行的行通过遍历 reader 对象 (spamreader) :

>>> import csv
>>> with open('al.csv', 'rb') as csvfile:
... spamreader = csv.reader(csvfile, delimiter=',')
... for row in spamreader:
print re.sub('[^A-Za-z0-9\.]+',' ',row[2]) + row[0]

关于python - 如何从一行之间删除换行符而不从行尾删除换行符python?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31008201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com