Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
tar归档中的Python读取文件_Python_Tar - Fatal编程技术网

tar归档中的Python读取文件

tar归档中的Python读取文件,python,tar,Python,Tar,我有一个文件:“docs.tar.gz”。tar文件有4个文件,其中第四个文件是“docs.json”,这是我所需要的。我可以通过以下方式查看tar文件的内容: import tarfile tar=tarfile.open("docs.tar.gz") tar.getmembers() 如何读取第四个文件-我需要的json文件?。提取内容后我无法继续。谢谢 试试这个: import tarfile tar = tarfile.open("docs.tar.gz") f = tar.extra

我有一个文件:“docs.tar.gz”。tar文件有4个文件,其中第四个文件是“docs.json”,这是我所需要的。我可以通过以下方式查看tar文件的内容:

import tarfile
tar=tarfile.open("docs.tar.gz")
tar.getmembers()
如何读取第四个文件-我需要的json文件?。提取内容后我无法继续。谢谢

试试这个:

import tarfile
tar = tarfile.open("docs.tar.gz")
f = tar.extractfile("docs.json")

# do something like f.read()
# since your file is json, you'll probably want to do this:

import json
json.loads(f.read())

这个也行

import tarfile
tar = tarfile.open("docs.tar.gz")
files = tar.getmembers()
f = tar.extractfile(files[0]) # if your docs.json is in the 0th position
f.readlines()

作为使用Python3上下文管理器的示例,JSON文件如下:

$ cat myfile.json
{
    "key1": 1,
    "key2": 2,
    "key3": null
}
被压缩

tar czvf myfile.json.tar.gz myfile.json
可以这样提取

import tarfile
import json

tar_file_name = "myfile.json.tar.gz"
data_file_name = "myfile.json"
with tarfile.open(tar_file_name, "r:gz") as tar:
    with tar.extractfile(data_file_name) as f:
        j = json.loads(f.read())

print(j)
# {'key1': 1, 'key2': 2, 'key3': None}

也许这会很有用。请注意,创建名为
file
的变量不是一个好做法,它是由PythonFile“/usr/lib/python2.7/gzip.py”执行的,第312行,在_readuncompress=self.decompress.decompress(buf)中错误:解压时出现错误-3:无效的文字/长度代码这是我在读取json文件时遇到的错误看起来像是一个损坏的文件@ashwinshanker@nathancahill..看起来文件太大,无法打开,谢谢!