【问题标题】:Filter twitter files by location按位置过滤 twitter 文件
【发布时间】:2015-06-11 06:31:13
【问题描述】:

我正在尝试查找大量推文的经纬度信息。在 json 推文中,一条推文纬度/经度数据的路径是,

{u'location: {u'geo': {u'coordinates : [120.0,-5.0]}}}

如果此位置路径存在,我希望能够检查每条推文。如果是这样,那么我想稍后在函数中使用该信息。如果没有,那么我想检查另一个位置路径,最后转到下一条推文。

这是我目前检查此路径是否存在以及是否有相应数据的代码。 'data' 是我使用 data.append(json.loads(line)) 方法打开的 twitter 文件列表。

counter = 0
for line in data:
    if u'coordinates' in data[counter][u'location'][u'geo']:

        print counter, "HAS FIELD"
        counter += 1
    else:
        counter += 1
        print counter, 'no location data'

我收到此代码的 KeyError 错误。如果我只是执行下面的代码,它可以工作,但不够具体,无法真正让我获得我需要的信息。

counter = 0
for line in data:
    if u'location' in data[counter]:

        print counter, "HAS FIELD"
        counter += 1
    else:
        counter += 1
        print counter, 'no location data'

有没有人有办法做到这一点。

下面是关于我整体工作的更多背景信息,但以上总结了我遇到的问题。

背景:我可以访问通过 gnip 购买的 120 亿条推文,这些推文分为多个文件。我试图一一梳理这些推文,找出哪些有位置(纬度/经度)数据,然后查看相应的坐标是否在某个国家/地区。如果该推文确实属于该国家/地区,我会将其添加到一个新数据库中,该数据库是我更大数据库的一个子集。

我已经成功创建了一个函数来测试纬度/经度是否落在目标国家/地区的边界框中,但是由于 2 个原因,我很难为每条推文填充纬度/经度。 1)如果存在的话,每个json文件中都有多个地方存储long/lat数据。 2) 推文被组织在一个复杂的字典中,我很难翻阅。

我需要能够遍历每条推文并查看是否存在针对不同位置路径的特定纬度/经度组合,以便我可以将其拉出并将其输入到我的函数中,以测试该推文是否源自我所在的国家/地区兴趣。

【问题讨论】:

    标签: python json twitter dictionary key


    【解决方案1】:

    我发现的解决方案可能不是最有效的,但很实用。它使用嵌套在 try-except 语句中的 if 语句。这使我可以检查不同的位置路径,但通过 KeyError 推送,以便我可以转到其他推文和路径。下面是我的代码。它会检查多条推文,并检查在 3 条路径中的任何一条中具有可用的经纬度组合的推文。它与我的 addTOdb 函数一起使用,该函数检查该 Lat/Long 组合是否在我的目标国家/地区。它还构建了一个名为 Lat Long 的单独字典,我可以在其中查看所有包含 Lat/Long 组合的推文以及我将它们拉到的路径。

    #use try/except function to see if entry is in json files
    #initialize counter that numbers each json entry
    counter = 0
    #This is a test dict to see what lat long was selected
    Lat_Long = {}
    for line in data:
        TweetLat = 0
        TweetLong = 0
        #variable that will indicate what path was used for coordinate lat/long
        CoordSource = 0
        #Sets while variable to False.  Will change if coords are found.
        GotCoord = False
        while GotCoord == False:
            #check 1st path using geo function
            try:
                
                if u'coordinates' in data[counter][u'geo'] and GotCoord == False:
                    TweetLat = data[counter][u'geo'][u'coordinates'][0]
                    TweetLong = data[counter][u'geo'][u'coordinates'][1]
                    #print 'TweetLat',TweetLat
                    print counter, "HAS FIELD"
                    addTOdb(TweetLat,TweetLong,North,South,East,West)
                    CoordSource = 1
                    GotCoord = True
            except KeyError:
                pass
            #check 2nd path using gnip info
            try:
                if u'coordinates' in data[counter][u'gnip'][u'profileLocations'][0][u'geo'] and GotCoord == False:
                    TweetLat = data[counter][u'gnip'][u'profileLocations'][0][u'geo'][u'coordinates'][1]
                    TweetLong = data[counter][u'gnip'][u'profileLocations'][0][u'geo'][u'coordinates'][0]
                    print counter, "HAS FIELD"
                    addTOdb(TweetLat,TweetLong,North,South,East,West)
                    CoordSource = 2
                    GotCoord = True
            except KeyError:
                pass
            #check 3rd path using location polygon info
            try:     
                if u'coordinates' in data[counter][u'location'][u'geo'] and GotCoord == False:
                    TweetLat = data[counter][u'location'][u'geo'][u'coordinates'][0][0][1]
                    TweetLong = data[counter][u'location'][u'geo'][u'coordinates'][0][0][0]
                    print counter, "HAS FIELD"
                    addTOdb(TweetLat,TweetLong,North,South,East,West)
                    CoordSource = 3
                    GotCoord = True
            except KeyError:
                pass
             
            if GotCoord==True:
                Lat_Long[counter] = [CoordSource,TweetLat, TweetLong]
            else:
                print counter, "no field"
                GotCoord = True       
        counter += 1    

    【讨论】:

      【解决方案2】:

      我收到此代码的 KeyError 错误

      假设键应该用双引号引起来,因为它们有':

      counter = 0
      for line in data:
          if "u'coordinates" in data[counter]["u'location"]["u'geo"]:
      
              print counter, "HAS FIELD"
              counter += 1
          else:
              counter += 1
              print counter, 'no location data'
      

      【讨论】:

      • 我检查了它,但它似乎不起作用。实际的键是“坐标”。当我读取其中的文件时,它变成了 u'coordinates',因为它是 Unicode。在我的第二个示例中,我使用键 u'location',它是 data[counter] 中的顶层键,它工作正常。似乎我无法调用子层字典键。也许它不被严格认为是关键?我已经尝试过 try-except,它似乎有效,因为它允许我通过 KeyError 错误,但我不知道它是否像 if-else 语句一样工作,这似乎是检查多个位置路径的最佳方法。
      • 我检查了以下路径: u'coordinates' in data[counter][u'location'][u'geo'] 我得到了一个 True 声明,但是它仅在该特定推文中存在密钥时才有效。对于没有该路径的推文,我没有收到 False 声明,而是收到 KeyError。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 2021-06-27
      • 2017-09-13
      • 1970-01-01
      相关资源
      最近更新 更多