Python3:尝试使用列表来;“过滤器”;通过JSON文件来检索所选信息
所以我试图解决这个问题-我有一个充满Python3:尝试使用列表来;“过滤器”;通过JSON文件来检索所选信息,python,json,python-3.x,Python,Json,Python 3.x,所以我试图解决这个问题-我有一个充满属性的文本文件。就像这样。。。(实际上,大约有100行属性) 我还有另外100个json文件,看起来像这样: [{ "Title": "painting", "Artist":"Pablo Picasso", "Width":400, "Height": 400, "Name": "test-painting", "Comment":"none" }] 我想尝试“过滤”json文件,以便它们只在textfile
属性的文本文件
。就像这样。。。(实际上,大约有100行属性)
我还有另外100个json文件,看起来像这样:
[{
"Title": "painting",
"Artist":"Pablo Picasso",
"Width":400,
"Height": 400,
"Name": "test-painting",
"Comment":"none"
}]
我想尝试“过滤”json
文件,以便它们只在textfile中显示定义的属性。例如,我希望示例json
文件在将textfile应用为过滤器后看起来像这样
[{
"Width":400,
"Height":400,
"Comment":"none"
}]
所以这个过滤器基本上只显示我在textfile中定义的内容。到目前为止,我已经尝试过了,但没有成功
import os,sys
import json
attributes = [word.strip('\n').split(',')
for word in open("textfile", 'r').readlines()]
for filename in os.listdir(.)
with open(filename) as jsonfile:
data = json.load(jsonfile)
for line in data:
for a in attribute:
if a in line:
print(line)
谢谢 看起来属性文件的读取方式有问题。
下面是一些基于属性过滤器文件提取json对象的代码
示例运行
./53619747.py -h
usage: 53619747.py [-h]
attributes-text-path json-input-directory
json-output-directory
positional arguments:
attributes-text-path
json-input-directory
json-output-directory
optional arguments:
-h, --help show this help message and exit
./53619747.py attributes.txt input-json output-json
示例输出
./53619747.py attributes.txt input-json output-json
('attributes.txt', 'input-json', 'output-json')
[['Color'], ['Width'], ['Height'], ['Software'], ['Comment'], ['Size']]
['Color', 'Width', 'Height', 'Software', 'Comment', 'Size']
input-json/0001.json
[{u'Comment': u'none', u'Name': u'test-painting', u'Artist': u'Pablo Picasso', u'Title': u'painting', u'Height': 400, u'Width': 400}]
{u'Comment': u'none', u'Name': u'test-painting', u'Artist': u'Pablo Picasso', u'Title': u'painting', u'Height': 400, u'Width': 400}
(u'Comment', u'none')
(u'Name', u'test-painting')
(u'Artist', u'Pablo Picasso')
(u'Title', u'painting')
(u'Height', 400)
(u'Width', 400)
{u'Comment': u'none', u'Width': 400, u'Height': 400}
output-json/0001.json
53619747.py的内容
#!/usr/bin/env python
import argparse
import os
import json
parser = argparse.ArgumentParser()
parser.add_argument("attributes-text-path")
parser.add_argument("json-input-directory")
parser.add_argument("json-output-directory")
args = vars(parser.parse_args())
print(args['attributes-text-path'],args['json-input-directory'],args['json-output-directory'])
attributes = [word.strip('\n').split(',')
for word in open(args['attributes-text-path'], 'r').readlines()]
print(attributes)
# [['Color'], ['Width'], ['Height'], ['Software'], ['Comment'], ['Size']]
attributes = []
with open(args['attributes-text-path'], 'r') as attributes_file_handler:
for line in attributes_file_handler:
# add the line minus the carriage return to attributes
attributes.append(line.strip())
print(attributes)
# ['Color', 'Width', 'Height', 'Software', 'Comment', 'Size']
for filename in os.listdir(args['json-input-directory']):
valid_json_file = False
input_json_objects = []
input_path = os.path.join(args['json-input-directory'],filename)
print(input_path)
with open(input_path) as input_json_file_handler:
try:
input_json_objects = json.load(input_json_file_handler)
valid_json_file = True
print(input_json_objects)
except Exception as exception:
print(exception)
if valid_json_file:
output_path = os.path.join(args['json-output-directory'],filename)
results = []
for item in input_json_objects:
print(item)
result = {}
attribute_found = False
for key in item.keys():
print(key,item[key])
if key in attributes:
result[key] = item[key]
attribute_found = True
if attribute_found:
print(result)
results.append(result)
print(output_path)
with open(output_path,'w') as output_json_file_handler:
output_json_file_handler.write(json.dumps(results,indent=4))
你的尝试失败了什么?它看起来确实是正确的,但您没有对匹配项采取操作(print()
基本上什么都不做)。将open(filename)作为jsonfile:我猜您忘记了在open参数中的“filename”之后指定“r”。如何区分JSON文件和属性文本文件?作为扩展?所以对于属性,我把txt文件作为一个列表,我得到了一个错误:TypeError:unhabable type:“list”调整了我的代码,并根据你的建议对其进行了编辑,效果非常好。再次感谢!
#!/usr/bin/env python
import argparse
import os
import json
parser = argparse.ArgumentParser()
parser.add_argument("attributes-text-path")
parser.add_argument("json-input-directory")
parser.add_argument("json-output-directory")
args = vars(parser.parse_args())
print(args['attributes-text-path'],args['json-input-directory'],args['json-output-directory'])
attributes = [word.strip('\n').split(',')
for word in open(args['attributes-text-path'], 'r').readlines()]
print(attributes)
# [['Color'], ['Width'], ['Height'], ['Software'], ['Comment'], ['Size']]
attributes = []
with open(args['attributes-text-path'], 'r') as attributes_file_handler:
for line in attributes_file_handler:
# add the line minus the carriage return to attributes
attributes.append(line.strip())
print(attributes)
# ['Color', 'Width', 'Height', 'Software', 'Comment', 'Size']
for filename in os.listdir(args['json-input-directory']):
valid_json_file = False
input_json_objects = []
input_path = os.path.join(args['json-input-directory'],filename)
print(input_path)
with open(input_path) as input_json_file_handler:
try:
input_json_objects = json.load(input_json_file_handler)
valid_json_file = True
print(input_json_objects)
except Exception as exception:
print(exception)
if valid_json_file:
output_path = os.path.join(args['json-output-directory'],filename)
results = []
for item in input_json_objects:
print(item)
result = {}
attribute_found = False
for key in item.keys():
print(key,item[key])
if key in attributes:
result[key] = item[key]
attribute_found = True
if attribute_found:
print(result)
results.append(result)
print(output_path)
with open(output_path,'w') as output_json_file_handler:
output_json_file_handler.write(json.dumps(results,indent=4))