python - 转储 twitter 数据,尝试处理 csv 文件,程序运行,但不处理
问题描述
所以首先我是python的新手。我将数据从 twitter 直接转储到 csv 文件。然后我希望使用程序读取该 csv 文件,并删除任何包含我未声明的信息的单元格。(在抓取数据时,您会收到很多不可用或不相关的信息)。代码不完整,但核心语法如下,我打算运行一个测试,看看它是否能满足我的要求。它运行程序并完成,但从未对我打开、读取和删除单元格的现有 csvfile 执行任何操作。
我的问题是,有人看到代码有什么问题吗?除了它未完成并且可以浓缩之外。我对 python 仍然只有一周的时间,并尝试边走边学。对于任何反馈,我们都表示感谢。
import csv
f = open('csvdata.csv', 'r+', newline= '')
reader = csv.DictReader(f, fieldnames=None , dialect='excel')
x = 0
y = 0
for row in reader:
try:
cell = row[y][x]
if 'created_at' in cell:
x = x+1
elif 'id:' in cell:
x = x+1
elif 'text:' in cell:
x = x+1
####elif box starts with '':
####x = x+1
####continue
elif 'source:' in cell:
x = x+1
elif 'user:{' in cell:
x = x+1
elif 'name:' in cell:
x = x+1
elif 'screen_name:' in cell:
x = x+1
elif 'location:' in cell:
x = x+1
elif 'url:' in cell:
x = x+1
elif 'description' in cell:
x = x+1
elif 'translator_type:' in cell:
x = x+1
elif 'protected' in cell:
x = x+1
elif 'verified' in cell:
x = x+1
elif 'followers' in cell:
x = x+1
elif 'friends' in cell:
x = x+1
elif 'listed' in cell:
x = x+1
elif 'favourites' in cell:
x = x+1
elif 'statuses' in cell:
x = x+1
elif 'time' in cell:
x = x+1
elif 'lang:' in cell:
x = x+1
elif 'is_translator' in cell:
x = x+1
elif 'default_profile' in cell:
x = x+1
elif 'notification' in cell:
x = x+1
elif 'geo:' in cell:
x = x+1
elif 'coordinates:' in cell:
x = x+1
elif 'place:' in cell:
x = x+1
elif 'contributors:' in cell:
x = x+1
elif 'quoted_status' in cell:
x = x+1
elif 'retweeted_status' in cell:
x = x+1
else:
del_cell
x = x+1
except:
y = y+1
解决方案
乍一看,您使用DictWriter
的是DictReader
. 然后作为调试步骤,尝试在读取行时打印出这些行,以确保您实际上正在读取正确的数据。
推荐阅读
- jquery - 删除 :target 滚动样式
- python - SpaCy:启用以前禁用的管道
- internationalization - 在 Flutter 应用程序中使用英里或公里?
- c# - 可空小数的 Dapper.FluentMap 错误
- c++98 - 最佳实践:在循环或普通数组访问中使用对对象的引用?
- amazon-web-services - 从 Apache AMI (AWS) 运行 docker 出现问题
- linux - Bash 脚本 Unix。模式匹配
- java - 离线时强制从 Cloud Firestore 缓存中提取
- excel - Excel ComboBox/Control 文本在活动时不显示/识别特殊字符,但在非活动时会显示/识别特殊字符
- python - 如何为需要比较同一类的自定义对象定义 __eq__ func