【问题标题】:Logic tests all values rather than one python逻辑测试所有值而不是一个 python
【发布时间】:2019-05-15 21:40:43
【问题描述】:

所以我有我的函数,它基本上检查两组数据中是否存在特定文件名。如果是这样,那么它将对文件大小进行一些计算并在终端上输出结果。由于我将一个文件名传递给 test ,因此它开始遍历列表中的每个文件名,直到完成。我只是想测试它的文件名'a.json'作为测试。然后我可以单独测试'b.json'和'c.json'。我目前得到的输出是:

a.json
()
(1000, 1000)
ok
b.json
()
(1000, 1000)
ok
c.json
()
(1000, 1000)
ok

所以伪代码将是:

For a.json in file_names
if a.json exists in jsonDatacurrFile
 if  a.json exist in both jsonDataprevFile and jsonDatacurrFile
  use compare function with the filesize from jsonDatacurrFile and jsonDataprevFile for a.json and output whatever condition it meets

所以一个示例输出是:

a.json - ok

文件如下:

jsonDataprevFile 等于:

{"File Name": "a.json", "File Size": 1000}
{"File Name": "b.json", "File Size": 1000}
{"File Name": "c.json", "File Size": 1000}

jsonDatacurrFile

{"File Name": "a.json", "File Size": 1000}
{"File Name": "b.json", "File Size": 1000}
{"File Name": "c.json", "File Size": 1000}    

我目前的逻辑如下:

def compare(previous,current):
  # temporary for debug
  print()
  print(previous,current)

  tolerance = 0.4

  if previous is None and current is None:
      return "both missing"

  if previous is None:
      return "new"

  if current is None:
      return "missing"

  size_ratio = float(current)/previous

  if size_ratio >= 1 + tolerance:
      return "not ok %d%% bigger" % round(((size_ratio - 1) * 100),0)

  if size_ratio <= 1 - tolerance:
      return "not ok %d%% smaller" % round(((1 - size_ratio) * 100),0)

  return "ok"



def readFileIntoDict(pathOfFile):
  fo = open(pathOfFile, "rw+")
  linesOfFiles = fo.readlines()
  dataInFile = {}
  for line in linesOfFiles:
      jsonD = json.loads(line)
      dataInFile[jsonD['File Name']] = jsonD['File Size']
  return dataInFile

  jsonDataprevFile = readFileIntoDict('dates/2018-01-01.json')
  jsonDatacurrFile = readFileIntoDict('dates/2018-01-02.json')


file_names = ['a.json', 'b.json', 'c.json']
for fileNames in file_names:
    if fileNames in jsonDatacurrFile:
        if jsonDataprevFile[fileNames] == jsonDatacurrFile[fileNames]:
         print fileNames
         print(compare(jsonDataprevFile.get('a.json') , jsonDatacurrFile.get('a.json')))

【问题讨论】:

  • 已修复@PatrickArtner

标签: python json python-2.7 list dictionary


【解决方案1】:

有几点可以提供帮助。你现在可能得到奇怪的答案,因为在这一行:

if jsonDataprevFile[fileNames] == jsonDatacurrFile[fileNames]:

您正在比较字典中的值(大小),而不是名称,因此只有在两者大小相同时才进入“if”块。此外,您在调用比较时使用了文字名称“a.json”,您应该在哪里使用 fileName 变量。

如果您要查找一组特定的名称,那么一种更简洁的方法是使用dict.keys() 方法为每个名称获取一组键,并在它们上使用集合交集来获取常见的键...

names_of_interest = {'a.file', 'b.file'}
names_in_both = json_file_a.keys() & json_file_b.keys()
# find names of interest that are in both files...
names = names_of_interest & names_in_both
# now you can just iterate through that set and go to work....
for name in names:
  compare(json_file_a[name], json_file_b[name])

请注意,如果您只想处理所有常用名称,则可以转储感兴趣的名称列表并仅处理键集的交集

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-05-20
    • 1970-01-01
    • 2017-10-18
    • 2021-04-28
    • 2021-03-30
    相关资源
    最近更新 更多