【问题标题】:Converting a large JSONL (32gb) of Twitter data into a CSV or dataframe (R or Python)将 Twitter 数据的大型 JSONL (32gb) 转换为 CSV 或数据帧(R 或 Python)
【发布时间】:2021-07-23 22:42:04
【问题描述】:

我使用 Hydrator 应用程序下载了一个大型 Twitter 数据集。该应用程序将 JSONL 转换为 CSV,但是,该应用程序丢弃了大量案例。我没有尝试将原始 JSONL 转换为 CSV。

使用 R 读取文件时,即使在流式传输推文时也会遇到内存错误:

library(jsonlite)
tweets_df <- stream_in(file("Compiled_Tweets.jsonl"),pagesize = 10000)
#Error:cannot allocate vector of size 5 Kb

使用从 stackoverflow 上的同一线程获取的不同方法来克服内存问题,我得到一个向量错误,我不确定如何解决:

file_name<-"Compiled_Tweets.jsonl"
tweet_df<-jsonlite::stream_in(textConnection(readLines(file_name, n=100000)),verbose = F)
attribute [1] must be the same length as the vector [0]

在朋友的推荐下,我尝试使用 Pandas,因为它被认为是内存高效的,但是在读取 JSONL 时我仍然遇到内存错误:

df = pd.read_json(r'C:\Users\tomed\Documents\Dissertation\Compiled_Tweets.jsonl')
df.to_csv (r'C:\Users\tomed\Documents\Dissertation\tweet_df.csv', index = None)
---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
<ipython-input-15-1efdeac4ca20> in <module>
----> 1 df = pd.read_json(r'C:\Users\tomed\Documents\Dissertation\Compiled_Tweets.jsonl')
      2 df.to_csv (r'C:\Users\tomed\Documents\Dissertation\tweet_df.csv', index = None)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    197                 else:
    198                     kwargs[new_arg_name] = new_arg_value
--> 199             return func(*args, **kwargs)
    200 
    201         return cast(F, wrapper)

C:\ProgramData\Anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    294                 )
    295                 warnings.warn(msg, FutureWarning, stacklevel=stacklevel)
--> 296             return func(*args, **kwargs)
    297 
    298         return wrapper

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\json\_json.py in read_json(path_or_buf, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression, nrows)
    595     )
    596 
--> 597     json_reader = JsonReader(
    598         filepath_or_buffer,
    599         orient=orient,

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\json\_json.py in __init__(self, filepath_or_buffer, orient, typ, dtype, convert_axes, convert_dates, keep_default_dates, numpy, precise_float, date_unit, encoding, lines, chunksize, compression, nrows)
    678 
    679         data = self._get_data_from_filepath(filepath_or_buffer)
--> 680         self.data = self._preprocess_data(data)
    681 
    682     def _preprocess_data(self, data):

C:\ProgramData\Anaconda3\lib\site-packages\pandas\io\json\_json.py in _preprocess_data(self, data)
    689         """
    690         if hasattr(data, "read") and (not self.chunksize or not self.nrows):
--> 691             data = data.read()
    692         if not hasattr(data, "read") and (self.chunksize or self.nrows):
    693             data = StringIO(data)

C:\ProgramData\Anaconda3\lib\codecs.py in decode(self, input, final)
    320         # decode input (taking the buffer into account)
    321         data = self.buffer + input
--> 322         (result, consumed) = self._buffer_decode(data, self.errors, final)
    323         # keep undecoded input until the next call
    324         self.buffer = data[consumed:]

MemoryError: 

我尝试同时使用 ijson 和 bigjson,但配置它们超出了我的技术能力,因为我是编码新手。有没有办法配置上述脚本之一,读取 JSON 以便我可以制作数据框,或者将其转换为 CSV?

感谢您的宝贵时间。




 

【问题讨论】:

  • 令我震惊的是,您需要一个类似于使用 SAX 处理无法放入内存的大型 XML 文档的流式 API。没用过,不过这个库提供了这些功能pypi.org/project/python-rapidjson
  • 32 GB 在笔记本电脑上?嗯......听起来你可能正试图通过花园软管挤压高尔夫球。考虑使用像 Spark 这样可以在磁盘上工作的工具可能是值得的。您可以将json 读入spark,通过explode 函数将其取消嵌套,然后将其写为CSV。查看此链接:spark.apache.org/docs/2.4.3/sql-data-sources-json.html

标签: python r pandas dataframe twitter


【解决方案1】:

有点作弊,但它可能适用于可能访问资源的人。由于这是大学项目的一部分,因此有人能够在一台功能强大的计算机上使用 json2csv.py 实用程序将 JSON 转换为 CSV。

编辑:感谢那些提供替代答案的人,我会在没有截止日期时检查它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-29
    • 2015-01-22
    • 2013-04-12
    • 2019-01-17
    • 2016-02-18
    • 2021-08-21
    • 2019-01-16
    相关资源
    最近更新 更多