【问题标题】:read many json files to find common key value pair python读取许多json文件以找到公共键值对python
【发布时间】:2017-07-22 18:39:39
【问题描述】:

我有一个 json 文件的路径列表。

files = ['/Users/sbm/Downloads/ds214mb/sub-EESS001/sub-EESS001_task-Cyberball_bold.json',
 '/Users/sbm/Downloads/ds214mb/sub-EESS002/func/sub-EESS002_task-Cyberball_bold.json',
 '/Users/sbm/Downloads/ds214mb/sub-EESS003/sub-EESS003_task-Cyberball_bold.json',
 '/Users/sbm/Downloads/ds214mb/sub-EESS004/func/sub-EESS004_task-Cyberball_bold.json',
 '/Users/sbm/Downloads/ds214mb/sub-EESS005/sub-EESS005_task-Cyberball_bold.json',
 '/Users/sbm/Downloads/ds214mb/sub-EESS006/sub-EESS006_task-Cyberball_bold.json',
 '/Users/sbm/Downloads/ds214mb/sub-EESS007/func/sub-EESS007_task-Cyberball_bold.json',
 '/Users/sbm/Downloads/ds214mb/sub-EESS008/func/sub-EESS008_task-Cyberball_bold.json']

现在我打算将所有这些文件读入与文件名或差异名称同名的字典中。然后遍历这些 dict 以找到共同的键值对。

我执行以下操作来读取所有 json 文件以区分 dict。现在,比较所有这些 dict 以找到公共键:值对的有效方法是什么?

import json
for file in range(0, len(files)):
    globals()['json%s' % file] = "Hello"

i = 0
for file in files:
    globals()['json%s' % i] = json.loads(open(file).read())
    i = i+1

示例 json 文件如下所示:

{
 'Manufacturer': 'Siemens',
 'ManufacturerModelName': 'Magnetom Verio',
 'RepetitionTime': 1.56,
 'SliceTiming': [0.0,
  0.78,
  0.06,
  0.84,
  0.12],
 'TaskName': 'Cyberball'}

【问题讨论】:

标签: python json dictionary


【解决方案1】:

有趣的问题....

我从管道 JSON 文件列表开始 ....

find <dir> | grep json$ 

该管道被发送到 python 程序....

所以现在看起来像

find <dir> | grep json$ | python t.py

python 代码执行以下操作

  1. 打开文件
  2. 读取文件
  3. JSON 解析成 Python 字典
  4. 输出 python 字典

所以这看起来像这样(Python3 代码)

import json,sys,pprint
for file in sys.stdin:
  file=file.strip('\n')
  with open(file,"rt") as ifp:
    b=ifp.read()
    b=(b.replace('\n','')).replace("'","\"")
  ifp.close()
  c=json.loads(b)
  for k,v in c.items():
    print('{}:{}'.format(k,v))

我们现在使用 bash 对输出进行排序和计数...一般看起来像这样...

sort | uniq -c | sort -n  

因此,将所有这些放在一起,我们得到......(我假设所有 JSON 都在与我目前相同的目录中)

ls *.json | python t.py | sort  | uniq -c  | sort -n

如果你想要前 5 名 - 它就变成了

ls *.json | python t.py | sort  | uniq -c  | sort -n | head -n 5

【讨论】:

  • 这里的问题是我打算在 python 中这样做
  • 好的...我将发布一个完整的Python方式
【解决方案2】:

仅在 python 中 - 没有 linux

files=['data1.json','data2.json','data3.json']
master_key_plus_value={}
import json,sys,pprint
for file in files:
  with open(file,"rt") as ifp:
    b=ifp.read()
    b=(b.replace('\n','')).replace("'","\"")
  ifp.close()
  c=json.loads(b)
  for k,v in c.items():
    if str(k)+': '+str(v) in master_key_plus_value:
        master_key_plus_value[str(k)+': '+str(v)] += 1
    else:
        master_key_plus_value[str(k)+': '+str(v)] = 1

#Now we have ready all the key + values into a single dictionary
#Sort by the value (occurance)



master_key

sorted_dictionary = sorted(master_key_plus_value.items(), key=lambda x: -x[1])

print("Most Common Key-Value is  {} Occurance {} ".format(sorted_dictionary[0][0],sorted_dictionary[0][1]))

同样的原则 对于每个文件 将 JSON 文件读取为文本 重新格式化并制作提供 python 字典的 Json 对象 组合键 + 值并与主字典进行比较 如果有,值加 1 别的 存储并将值设置为 1 最后按值降序排序 打印顶部元素 ([0]) 这是一个元组,因此它是 [0][0] 和 [0][1]

【讨论】:

    猜你喜欢
    • 2019-05-14
    • 2014-05-02
    • 2017-07-20
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多