我正在尝试读取以以下格式保存在csv文件中的已处理推文字符串:
rt mayasolovely woman complain cleaning house amp man always take trash,momma said pussy cats inside doghouse,addicted2guys simplyaddictedtoguys http co 1jl4hi8zmf woof woof hot scally lad,allaboutmanfeet http co 3gzupfumev woof woof hot soles
每条推文都用逗号分隔,我想将它们全部传递给一维字符串列表。我尝试了以下代码:
X_new = []
filename = 'output.csv'
with open(filename, 'r') as f:
reader = csv.reader(f)
for row in reader:
X_new.append(row)
print(X_new)
我得到的结果如下。
[['rt mayasolovely woman complain cleaning house amp man always take trash', 'momma said pussy cats inside doghouse', 'addicted2guys simplyaddictedtoguys http co 1jl4hi8zmf woof woof hot scally lad', 'allaboutmanfeet http co 3gzupfumev woof woof hot soles']]
我不明白为什么它会给我2D字符串。我希望它能够返回此:
['rt mayasolovely woman complain cleaning house amp man always take trash', 'momma said pussy cats inside doghouse', 'addicted2guys simplyaddictedtoguys http co 1jl4hi8zmf woof woof hot scally lad', 'allaboutmanfeet http co 3gzupfumev woof woof hot soles']
出什么事了,谢谢!
答案 0 :(得分:1)
一行是一个列表,您尝试添加一行,而不是此行,请尝试使用该行扩展原始列表。
X_new = []
filename = 'output.csv'
with open(filename, 'r') as f:
reader = csv.reader(f)
for row in reader:
X_new.extend(row)
print(X_new)