首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >改进用于大数据集的Twitter解析器

改进用于大数据集的Twitter解析器
EN

Code Review用户
提问于 2017-03-30 02:57:40
回答 1查看 1.1K关注 0票数 3

我有以下完全工作的代码

  1. 导入JSON文件,
  2. 分析JSON中包含的tweet,
  3. 将它们记录在数据帧中的表中。

考虑到我目前分析了1,400个JSON(大约1.5Gb),运行代码需要相当长的时间。请建议是否有一个合理的方法来优化代码,以提高其速度。谢谢!

代码语言:javascript
运行
复制
import os
import json
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os

tweets = []

for dirs, subdirs, files in os.walk('/Users/mymac/Documents/Dir'):
    for file in files:
        if file.endswith('.json'):
            print(file)
            for line in open(file) :
                try:
                    tweet = json.loads(line)
                    tweets.append(tweet)
                except:
                    continue

tweet = tweets[0]

ids = [tweet['id_str'] for tweet in tweets if 'id_str' in tweet] 
text = [tweet['text'] for tweet in tweets if 'text' in tweet]
lang = [tweet['lang'] for tweet in tweets if 'lang' in tweet]
geo = [tweet['geo'] for tweet in tweets if 'geo' in tweet]                    
place = [tweet['place'] for tweet in tweets if 'place' in tweet]

df=pd.DataFrame({'Ids':pd.Index(ids),
                 'Text':pd.Index(text),
                 'Lang':pd.Index(lang),
                 'Geo':pd.Index(geo),
                 'Place':pd.Index(place)})
df
EN

回答 1

Code Review用户

回答已采纳

发布于 2017-03-30 07:03:41

只是简单地考虑一下:

  • 你有两次import os
  • 你不使用matplotlibnumpy,所以imports可以去
  • tweet = tweets[0]是无用的
  • 您不是要关闭打开的文件,应该使用with关键字

两个优化:

  • 我会移除print(file)。这可能是你能做的一个最好的优化。
  • 你已经循环过一次了,为什么还要再循环五次呢?

像这样的东西怎么样(没有测试!):

代码语言:javascript
运行
复制
from collections import defaultdict

elements_keys = ['ids', 'text', 'lang', 'geo', 'place']
elements = defaultdict(list)

for dirs, subdirs, files in os.walk('/Users/mymac/Documents/Dir'):
    for file in files:
        if file.endswith('.json'):
            with open(file, 'r') as input_file:
                for line in input_file:
                    try:
                        tweet = json.loads(line)
                        for key in elements_keys:
                            elements[key].append(tweet[key])
                    except:
                        continue

df=pd.DataFrame({'Ids': pd.Index(elements['id']),
                 'Text': pd.Index(elements['text']),
                 'Lang': pd.Index(elements['lang']),
                 'Geo': pd.Index(elements['geo']),
                 'Place': pd.Index(elements['place'])})
df
票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/159303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档