挖掘数据到csv文件,现在我想处理我希望保留的数据



首先,我从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 行中。

我看到你正在使用python的csv库。在文档中,他们有一些关于如何读取数据的很好的例子!

这是有关该主题的一些文档!

似乎您只想读取数据,并将某些类型的数据加载到看似字典中。我推荐字典,这样您就可以轻松检索"lang"或"place"等数据。

下面是 csv 库文档中的示例

>>> import csv
>>> with open('names.csv') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])

为了编辑此示例,我会读入字典而不是打印出每一行。

这是python中的字典文档!(这是页面向下的一种方式)

希望这有助于阐明这个主题,有时使用非结构化csv非常困难。

最新更新