- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 tweepy 的 Streamlistener 来收集 Twitter 数据,我使用的代码会生成一个包含一堆元数据的 JSONL 文件。现在我想将该文件转换为 CSV,我为此找到了一个代码。不幸的是我遇到了错误阅读:
raise JSONDecodeError("Extra data", s, end)
json.decoder.JSONDecodeError: Extra data: line 2 column 1 (char 7833)
我已经阅读了其他线程,我认为这与 json.loads
无法处理 json 文件中的多个数据部分有关(这当然是我的情况) json 列表文件)。我如何在代码中规避这个问题?或者我是否必须使用完全不同的方法来转换文件? (我使用的是 python 3.6,我流式传输的推文大部分是阿拉伯语)。
__author__ = 'seandolinar'
import json
import csv
import io
'''
creates a .csv file using a Twitter .json file
the fields have to be set manually
'''
data_json = io.open('stream_____.jsonl', mode='r', encoding='utf-8').read() #reads in the JSON file
data_python = json.loads(data_json)
csv_out = io.open('tweets_out_utf8.csv', mode='w', encoding='utf-8') #opens csv file
fields = u'created_at,text,screen_name,followers,friends,rt,fav' #field names
csv_out.write(fields)
csv_out.write(u'\n')
for line in data_python:
#writes a row and gets the fields from the json object
#screen_name and followers/friends are found on the second level hence two get methods
row = [line.get('created_at'),
'"' + line.get('text').replace('"','""') + '"', #creates double quotes
line.get('user').get('screen_name'),
unicode(line.get('user').get('followers_count')),
unicode(line.get('user').get('friends_count')),
unicode(line.get('retweet_count')),
unicode(line.get('favorite_count'))]
row_joined = u','.join(row)
csv_out.write(row_joined)
csv_out.write(u'\n')
csv_out.close()
最佳答案
如果数据文件由多行组成,每行都是一个 json 对象,您可以使用生成器一次解码一行。
def extract_json(fileobj):
# Using "with" ensures that fileobj is closed when we finish reading it.
with fileobj:
for line in fileobj:
yield json.loads(line)
对代码的唯一更改是未显式读取 data_json
文件,并且 data_python
是调用 extract_json
而不是的结果json.loads
。修改后的代码如下:
import json
import csv
import io
'''
creates a .csv file using a Twitter .json file
the fields have to be set manually
'''
def extract_json(fileobj):
"""
Iterates over an open JSONL file and yields
decoded lines. Closes the file once it has been
read completely.
"""
with fileobj:
for line in fileobj:
yield json.loads(line)
data_json = io.open('stream_____.jsonl', mode='r', encoding='utf-8') # Opens in the JSONL file
data_python = extract_json(data_json)
csv_out = io.open('tweets_out_utf8.csv', mode='w', encoding='utf-8') #opens csv file
fields = u'created_at,text,screen_name,followers,friends,rt,fav' #field names
csv_out.write(fields)
csv_out.write(u'\n')
for line in data_python:
#writes a row and gets the fields from the json object
#screen_name and followers/friends are found on the second level hence two get methods
row = [line.get('created_at'),
'"' + line.get('text').replace('"','""') + '"', #creates double quotes
line.get('user').get('screen_name'),
unicode(line.get('user').get('followers_count')),
unicode(line.get('user').get('friends_count')),
unicode(line.get('retweet_count')),
unicode(line.get('favorite_count'))]
row_joined = u','.join(row)
csv_out.write(row_joined)
csv_out.write(u'\n')
csv_out.close()
关于python - 将 JSONL 文件转换为 CSV - "JSONDecodeError: Extra data",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46648387/
我正在将 ansible 合并到我们的内部 RHN 卫星中。我有来自 Red Hat 的完全更新的 RHEL 7 基础 repo 、附加、补充、可选和 RHN 工具。当我们需要它们时,我还会将选定的包
我尝试将一些值发送到另一个 Activity 。 recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicatio
我有这个代码: Intent intent = new Intent(); intent.setAction(Intent.ACTION_PICK); intent.s
几天前,我一直在努力寻找一种方法来为我的闹钟使用自定义 Intent 。虽然我得到了明确的答案,但我必须根据一些唯一的 ID 来定制 Intent,例如。 setAction()还是有一些问题。 我这
通知正在提供旧值。我阅读了 stackoverflow 链接,但仍然不适合我: Notification passes old Intent Extras 我有一个 Activity A。当我在 Ac
我想写以下内容: result = [] for x in list(range(10)): if x%2 != 0: for a in [1,2]:
我在使用 mysql 5.5.12 时遇到了 Amazon RDS 的 IO 性能问题。有 2 种实例类型相似且价格接近: 超大数据库实例:15 GB 内存、8 个 ECU(4 个虚拟核心,每个 2
这里是 Android 的新手,我正在与一位资深人士就 bundle 和 Intent 进行辩论。这就是我一直在做的...... Intent intent = new Intent(this, Ta
我使用的是MinGW+MSYS, 我添加了 extra-include-dirs、extra-lib-dirs,但似乎没有任何东西可以帮助 cabal 找到 PCRE 库。以下是我尝试过的一些命令行,
我已将 CVS 中的一个模块 check out 到新安装的 Windows 7 计算机上。病毒扫描程序尚未安装。 后来,当我尝试进行更新时,收到以下错误消息: 无法将文件 CVS/Entries.E
Alpine镜像中的telnet在3.7版本后被转移至busybox-extras包中,需要使用apk单独安装。 现象 Alpine版本为3.8, 不再有指向busybox的telent
对lazy="extra"究竟能做什么,有没有很好的解释? 我看过的所有帖子都只是重复了一个事实,即它会引用 MyObject.ItsCollection.Count进入 select count(*
这个问题已经有答案了: Two semicolons inside a for-loop parentheses (4 个回答) Endless loop in C/C++ [closed] (12
我的包为包含的额外内容返回空字符串。不是 NPE,是实际的“空”值。关于为什么会发生这种情况的任何想法? 新 bundle String u = nul
在 es6 中,以下似乎是有效代码: function test(a1,{a=1,b=2} = {},) {} 注意函数参数中额外的,。我不确定这是否是一个错误,因为这个额外的 , 仅适用于解构分配。
我正在查看一个包含 .myClass a.extra{...} 和 .myClass a.extra:hover{...} 的 css 模板“额外”是什么意思? 最佳答案 extra 是类名。 因为你
我来自 Web 开发的前端世界,我们非常努力地尝试限制发出的 HTTP 请求的数量(通过合并 css、js 文件、图像等)。 对于数据库连接 (MySQL),显然您不希望有不必要的连接,但作为一般规则
问题是关于包含不必要的 header 以避免在子文件中多次调用它。这是场景,我有几个文件: srlogger.h srinterface.h srinterface.cc #include #inc
我有一个程序表现出奇怪的行为 #include #include using namespace std; class man{ int i ; public:
本文整理了Java中org.threeten.extra.YearQuarter类的一些代码示例,展示了YearQuarter类的具体用法。这些代码示例主要来源于Github/Stackoverflo
我是一名优秀的程序员,十分优秀!