Python 脚本将 json 文件作为字符串导入并解析

Python script to import json file as a string and parse

我在下面有一个 python 脚本,当我在第二行中有一个 json 字符串时,我知道它可以工作。问题是如何将 json 文件 (output.json) 中的数据作为字符串导入第二行?

# JSON string

product = ' {contents of a json file go here} '

# Sanitize data
product = product.replace('\n', '').replace('\r', '')  

# Convert string to Python dict
product_dict = json.loads(str(product))

findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)

到目前为止我有这个....

import json

# Opening JSON file
with open('output.json', 'wb') as product:
     json.dumps(product)

print (product)

# Convert string to Python dict

product_dict = json.loads(str(product))

# Sanitize data
#product = product.replace('\n', '').replace('\r', '')

findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)

哪个报错:

Traceback (most recent call last):   File "/home/fxeduat/staticdatacheck1.py", line 5, in <module>
    json.dumps(product)   File "/opt/tech/releases/tools/python/current/lib/python3.9/json/__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)   File "/opt/tech/releases/tools/python/current/lib/python3.9/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)   File "/opt/tech/releases/tools/python/current/lib/python3.9/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)   File "/opt/tech/releases/tools/python/current/lib/python3.9/json/encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type BufferedWriter is not JSON serializable

所以现在我有

import json

# Opening JSON file
with open('output.json') as product:

# Convert string to Python dict
 product_dict = json.loads(str(product))

# Sanitize data
product = product.replace('\n', '').replace('\r', '')

findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)

但是这个错误

 Traceback (most recent call last):
      File "/home/fxeduat/staticdatacheck1.py", line 7, in <module>
        product_dict = json.loads(str(product))
      File "/opt/tech/releases/tools/python/current/lib/python3.9/json/__init__.py", line 346, in loads
        return _default_decoder.decode(s)
      File "/opt/tech/releases/tools/python/current/lib/python3.9/json/decoder.py", line 337, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/opt/tech/releases/tools/python/current/lib/python3.9/json/decoder.py", line 355, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

json 文件是否需要在其他地方?

@马蒂亚斯

import json

# Opening JSON file
with open('output.json') as f:
 product = json.load(f)
 print(product)

# Convert string to Python dict
product_dict = json.loads(str(product))

# Sanitize data
product = product.replace('\n', '').replace('\r', '')

findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)

错误

Traceback (most recent call last):
  File "/home/fxeduat/staticdatacheck1.py", line 5, in <module>
    product = json.load(f)
  File "/opt/tech/releases/tools/python/current/lib/python3.9/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/opt/tech/releases/tools/python/current/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/opt/celertech/releases/tools/python/current/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/opt/tech/releases/tools/python/current/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

@quamrana

import json

# Opening JSON file
with open('output.json') as product:
     product_dict = json.load(product)

# Sanitize data
product = product.replace('\n', '').replace('\r', '')

findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)

新错误

Traceback (most recent call last):
  File "/home/fxeduat/staticdatacheck1.py", line 13, in <module>
    product = product.replace('\n', '').replace('\r', '')
AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'

如果您已有文件 output.json,那么您可以使用 json 库来读取该文件:

import json

# Opening JSON file
with open('output.json') as product:
     product_dict = json.load(product)

fa = product_dict['hits']['hits']
...

我的回答:

import json

# Opening JSON file
with open('output.json') as product:
         product_dict = json.load(product)


findd = []

fa = product_dict['hits']['hits']
for every_item in fa:
    findd.append(every_item['_source']['securityId'])

print(findd)