【发布时间】:2020-03-06 17:42:01
【问题描述】:
我有一个 26 Gb 的文本文件,行格式如下
/type/edition /books/OL10000135M 4 2010-04-24T17:54:01.503315 {"publishers": ["Bernan Press"], "physical_format": "Hardcover", "subtitle": "9th November - 3rd December, 1992", "key": "/books/OL10000135M", "title": "Parliamentary Debates, House of Lords, Bound Volumes, 1992-93", "identifiers": {"goodreads": ["6850240"]}, "isbn_13": ["9780107805401"], "languages": [{"key": "/languages/eng"}], "number_of_pages": 64, "isbn_10": ["0107805405"], "publish_date": "December 1993", "last_modified": {"type": "/type/datetime", "value": "2010-04-24T17:54:01.503315"}, "authors": [{"key": "/authors/OL2645777A"}], "latest_revision": 4, "works": [{"key": "/works/OL7925046W"}], "type": {"key": "/type/edition"}, "subjects": ["Government - Comparative", "Politics / Current Events"], "revision": 4}
我试图只获取最后一列是 json 并且从那个 Json 我只是试图保存“title”、“isbn 13”、“isbn 10”
我只能用这段代码保存最后一列
csv.field_size_limit(sys.maxsize)
# File names: to read in from and read out to
input_file = '../inputFile/ol_dump_editions_2019-10-31.txt'
output_file = '../outputFile/output.txt'
## ==================== ##
## Using module 'csv' ##
## ==================== ##
with open(input_file) as to_read:
with open(output_file, "w") as tmp_file:
reader = csv.reader(to_read, delimiter = "\t")
writer = csv.writer(tmp_file)
desired_column = [4] # text column
for row in reader: # read one row at a time
myColumn = list(row[i] for i in desired_column) # build the output row (process)
writer.writerow(myColumn) # write it
但这不会返回正确的 json 对象,而是返回旁边带有双引号的所有内容。另外,我将如何从 json 中提取某些值作为新的 json
编辑:
"{""publishers"": [""Bernan Press""], ""physical_format"": ""Hardcover"", ""subtitle"": ""9th November - 3rd December, 1992"", ""key"": ""/books/OL10000135M"", ""title"": ""Parliamentary Debates, House of Lords, Bound Volumes, 1992-93"", ""identifiers"": {""goodreads"": [""6850240""]}, ""isbn_13"": [""9780107805401""], ""languages"": [{""key"": ""/languages/eng""}], ""number_of_pages"": 64, ""isbn_10"": [""0107805405""], ""publish_date"": ""December 1993"", ""last_modified"": {""type"": ""/type/datetime"", ""value"": ""2010-04-24T17:54:01.503315""}, ""authors"": [{""key"": ""/authors/OL2645777A""}], ""latest_revision"": 4, ""works"": [{""key"": ""/works/OL7925046W""}], ""type"": {""key"": ""/type/edition""}, ""subjects"": [""Government - Comparative"", ""Politics / Current Events""], ""revision"": 4}"
编辑 2:
所以我试图读取这个文件,它是一个制表符分隔的文件,包含以下列:
type - 记录类型(/type/edition、/type/work 等) key - 记录的唯一键。 (/books/OL1M 等) 修订 - 记录的修订号 last_modified - 最后修改的时间戳 JSON - JSON 格式的完整记录
我正在尝试读取 JSON 文件,而我只是试图从该 Json 中获取“title”、“isbn 13”、“isbn 10”作为 json 并将其作为一行保存到文件中
所以每一行都应该看起来像原来的,但只有那些键和值
【问题讨论】:
-
请分享您的代码返回的内容。
-
抱歉我更新了
标签: python json csv parsing text