python - 循环 JSON 到附加 CSV 的多文件处理
问题描述
我无法使用 for 循环的正确语法一次打开多个 gzip 行 json 文件,处理文件以仅提取某些键值对,然后保存到附加的单个 csv 文件。gzip 文件很大。
for循环应该如何嵌套一次打开,处理,写入一个文件?
我似乎在写入之前一次打开/加载所有文件,然后最终崩溃内存或一次成功打开 1 个但在写入阶段出现 I/O 错误,因为 outfile 已关闭。
directory = r"*/mydirectory*"
field_names = [
"id",
"created_at",
"user_screen_name",
"text",
"lang",
"place_country_code",
"place_name",
"coordinates",
"entities_user_mentions_screen_name",
]
tweets = []
for filename in os.scandir(directory):
if filename.path.endswith(".gz") and filename.is_file():
with gzip.open(filename, 'r') as infile, \
open('clean_tweet_all_data.csv', 'a', newline="", encoding='utf-8') as outfile:
for line in infile:
tweets.append(json.loads(line)),
csv_output = csv.DictWriter(outfile, delimiter=",", fieldnames=field_names,
extrasaction="ignore")
if outfile.tell() == 0:
csv_output.writeheader(),
csv_output.writerows(get_arrays(entry) for entry in tweets)
infile.close()
outfile.close()
该函数get_arrays(entry)
将 json 文件展平,因此field_names
用于选择键值对的列表运行良好。
解决方案
我看到了三个问题,但我不知道这是否会导致您描述的问题
列表一直在增长,这可能会导致内存错误,你应该在写完推文后
tweets
重置它( )tweets = []
最后不需要
infile.close()
andoutfile.close()
,这已经由上下文管理器 (with open ...
)您只写入文件一次,因为您只写入 if
outfile.tell() == 0
,也许您打算将最后一行放在if
块之外?
推荐阅读
- gchart - 谷歌图表饼图材料设计
- javascript - 如何使用正则表达式和 javascript 用强标签替换字符串中的模式?
- python - 陷入循环 - 线程继续,而循环阻止代码向前移动(Python)
- python - 通过取数据框 pandas 中的第一个值按对象取消分组
- xpath - 刮掉带有href的段落
- android - 安全访问数据库的最常用方法
- javascript - Javascript:getHtmlNavThumbTogglePanel 不是函数
- x86 - WinDbg 是否显示 [MOV r32,r/m32] 的错误操作码?
- jenkins - 我在哪里可以找到文件系统上 jenkins 配置中的“启用代理 → 主访问控制”选项?
- mysql - 如何加快 NOT IN 查询?