【发布时间】:2018-10-28 02:56:46
【问题描述】:
首先,我从 Twitter 中挖掘数据,并将其直接记录到 CSV 文件中。这部分进展顺利,但正如你们中的一些人已经知道的那样,当你挖掘 twitter 数据时,你得到的信息比你需要的要多得多。在下面的“代码”中,我已经绘制了我想要保留在 CSV 文件中的逻辑以及我想要丢弃的逻辑。 我对 python 非常非常陌生,一周前刚开始,没有接受过传统培训。我通过随机搜索了解了我所知道的内容,以帮助我弄清楚如何编写我需要做的事情。对于我挖掘的数据的处理,我不知道实现下面列出的逻辑所需的语法。数据按行记录,因为从 1 条推文中收集的所有信息都列在 1 行中,每列是收集的不同数据(第 1 列 =“在时间创建”,第 2 列 = ID,等等)。我想要做的是让程序从 CSV 文件的第 1 格开始读取,并遍历每个块并检查它是否符合我在下面列出的要求。如果该块包含条件中的任何文本,则保持原样并继续下一个,如果程序当前正在读取的框没有任何声明的文本条件,则删除该框我。
我知道我下面的不是正确的语法或代码,只是我在开始之前绘制了我想要执行的逻辑。
我正在寻找有关语法或结构的任何帮助。欢迎任何反馈、建议或问题。希望这也可以帮助其他试图处理他们收到的数据的人。
导入 csv 导入json
f = open('csvdata.csv', 'r+')
for line in f:
try:
f.readlines()
if box contains 'created_at':
continue (keep box)
elif box contains 'id:':
continue (keep box)
elif box contains 'text:':
continue (keep box)
elif box starts with '':
continue (keep box)
elif box contains 'source:':
continue (keep box)
elif box contains 'user:{':
continue (keep box)
elif box contains 'name:':
continue (keep box)
elif box contains 'screen_name:':
continue (keep box)
elif box contains 'location:':
continue (keep box)
elif box contains 'url:':
continue (keep box)
elif box contains 'description':
continue (keep box)
elif box contains 'translator_type:':
continue (keep box)
elif box contains 'protected':
continue (keep box)
elif box contains 'verified':
continue (keep box)
elif box contains 'followers':
continue (keep box)
elif box contains 'friends':
continue (keep box)
elif box contains 'listed':
continue (keep box)
elif box contains 'favourites':
continue (keep box)
elif box contains 'statuses':
continue (keep box)
elif box contains 'time':
continue (keep box)
elif box contains 'lang:':
continue (keep box)
elif box contains 'is_translator':
continue (keep box)
elif box contains 'default_profile':
continue (keep box)
elif box contains 'notification':
continue (keep box)
elif box contains 'geo:':
continue (keep box)
elif box contains 'coordinates:':
continue (keep box)
elif box contains 'place:':
continue (keep box)
elif box contains 'contributors:':
continue (keep box)
elif box contains 'quoted_status':
continue (keep box)
elif box contains 'retweeted_status':
continue (keep box)
else:
(delete box)
except:
continue
(编辑)- 我希望程序处理当前 CSV 文件中的数据并编辑预先存在的文件或创建一个全新的 CSV 文件并将我希望保存的信息写入其中。然而,第二个过程需要设置变量,这些变量将作为项目写入新文件,以便程序知道何时将第二条推文放入第 2 行,而不仅仅是将单独推文中的所有信息转储到 1 行中。
【问题讨论】: