简体   繁体   English

从 zip 存档中加载 json 文件

[英]Load json file from within a zip archive

Hi I want work on the last file in a zip archive.嗨,我想处理 zip 存档中的最后一个文件。

from zipfile import ZipFile 
from natsort import natsorted

with ZipFile('1.zip', 'r') as zipObj:
  abc = zipObj.namelist()
  abc =  natsorted(abc, key = str.lower) 
  a22 = abc[-1]  
a22

I got: "5550.json"我得到: "5550.json"

(btw I work only on numeric json from 1 to 5000-10000) (顺便说一句,我只处理从 1 到 5000-10000 的数字 json)

I created this code to get the last file in the zip archive, but when I try我创建了这段代码来获取 zip 存档中的最后一个文件,但是当我尝试

f = open(a22)
data = json.load(f) 
data 

but it's different path.但这是不同的路径。 Is there another method to get the contents of this json file?是否有另一种方法来获取此 json 文件的内容?

Open the file with the ZipFile.open method.使用ZipFile.open方法打开文件。

For example:例如:

from zipfile import ZipFile 
from natsort import natsorted

with ZipFile('1.zip', 'r') as zipObj:
  abc = zipObj.namelist()
  abc =  natsorted(abc, key = str.lower) 
  a22 = abc[-1]  
  info = json.load(zipObj.open(a22))
info

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM