我正在解析一个标签分隔的文件,其中第一个元素是twitter标签,第二个元素是tweet内容
我的输入文件如下所示:
#trumpisanabuser of young black men . calling for the execution of the innocent !url "
#centralparkfiv of young black men . calling for the execution of the innocent !url "
#trumppence16 "
#trumppence16 "
#america2that @user "
我的代码就是通过检查第二个tab分隔的元素是否是重复的来过滤重复的内容,比如转发
import sys
import csv
tweetfile = sys.argv[1]
tweetset = set()
with open(tweetfile, "rt") as f:
reader = csv.reader(f, delimiter = '\t')
for row in reader:
print("hashtag: " + str(row[0]) + "\t" + "tweet: " + str(row[1]))
row[1] = row[1].replace("\\ n", "").rstrip()
if row[1] in tweetset:
continue
temp = row[1].replace("!url","")
temp = temp.replace("@user","")
temp = "".join([c if c.isalnum() else "" for c in temp])
if temp:
taglines.append(row[0] + "\t" + row[1])
tweetset.add(row[1])
但是,解析过程很奇怪。当我打印每个解析的项目时,输出如下。有人能解释为什么解析中断并导致打印这一行(hashtag: #trumppence16 tweet:
,newline,然后是#trumppence16
)吗
hashtag: #centralparkfive tweet: of young black men . calling for the execution of the innocent !url "
hashtag: #trumppence16 tweet:
#trumppence16
hashtag: #america2that tweet: @user "
你的tweet有
"
行。CSV可以通过在值周围用"
引用列,包括换行符。从开始"
到下一个结束"
的所有内容都是一个列值您可以通过将^{} option 设置为^{} 来禁用引号处理:
相关问题 更多 >
编程相关推荐