【问题标题】:Mined Data to a csv file, now I want to process the data I wish to keep挖掘数据到 csv 文件,现在我想处理我希望保留的数据
【发布时间】: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 行中。

【问题讨论】:

    标签: python csv syntax


    【解决方案1】:

    我看到您正在使用 python 的 csv 库。在文档中,他们有一些很好的例子来说明如何读入数据!

    Here is some documentation on the topic!

    您似乎只想读入数据,并将某些类型的数据加载到看起来像字典的东西中。我推荐一本字典,这样您就可以轻松检索诸如“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'])
    

    要编辑这个例子,我会读入字典而不是打印出每一行。

    Here is dictionary documentation in python!(在页面下方)

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

    【讨论】:

    • 这两个链接都非常有帮助,谢谢。我能做的所有阅读也有助于未来的项目。
    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 2011-02-07
    • 2012-01-25
    • 2019-02-27
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    相关资源
    最近更新 更多