简体   繁体   English

如何使用Python对以数字文件名作为键的JSON文件名进行排序?

[英]How do I sort a JSON file name that has numeric file names as the key using Python?

I have a JSON file that has file names and a description for each as the key value pairs. 我有一个JSON文件,其中包含文件名和每个键值对的描述。 However using OrderedDict when I try to sort the file it results in the order 0.jpg 1.jpg 10.jpg 11 ..... 2 20 and so on. 但是,当我尝试对文件进行排序时,使用OrderedDict会导致顺序为0.jpg 1.jpg 10.jpg 11 ..... 2 20,依此类推。

op={int(k) : v for k, v in output.items()}

I tried doing this but it returns a ValueError. 我尝试这样做,但它返回ValueError。 The error is as follows 错误如下

ValueError: invalid literal for int() with base 10: '1520.png' ValueError:以10为底的int()的无效文字:'1520.png'

OrderedDict(sorted(output.items(), key=lambda item: int(item[0].split('.')[0])))

Example

output = {f'{i}.jpg': None for i in reversed(range(21))}

od = OrderedDict(sorted(output.items(), key=lambda item: int(item[0].split('.')[0])))

Output: 输出:

OrderedDict([('0.jpg', None),
             ('1.jpg', None),
             ('2.jpg', None),
             ('3.jpg', None),
             ('4.jpg', None),
             ('5.jpg', None),
             ('6.jpg', None),
             ('7.jpg', None),
             ('8.jpg', None),
             ('9.jpg', None),
             ('10.jpg', None),
             ('11.jpg', None),
             ('12.jpg', None),
             ('13.jpg', None),
             ('14.jpg', None),
             ('15.jpg', None),
             ('16.jpg', None),
             ('17.jpg', None),
             ('18.jpg', None),
             ('19.jpg', None),
             ('20.jpg', None)])

You're calling the int() function on a string that's not entirely made up of digits. 您正在对不完全由数字组成的字符串调用int()函数。 The dictionary comprehension you've built is going loops through each key/value pair in the original dict output and tries to cast each key to an integer. 您构建的字典理解将遍历原始dict output中的每个键/值对,并尝试将每个键转换为整数。

In your example the keys are filenames like "0.png", "1.jpg", "10.png", etc. and a string like '10.png' can't be turned in to an integer. 在你的榜样键是像“0.png”,“1.JPG”,“10.png”等,像一个字符串的文件名'10.png'不能打开到一个整数。 That's why you're getting the ValueError . 这就是为什么要获取ValueError的原因。

Ordering dictionaries in Python is a little tricky because we think everything should be based on a key's value when in fact Python orders dictionary keys based on their hash value. 在Python中对字典进行排序有点棘手,因为我们认为实际上Python应该根据其哈希值对字典键进行排序时,所有内容都应基于键的值。

As for using an OrderedDict, that container only remembers the order in which the keys were inserted. 至于使用OrderedDict,该容器仅记住键的插入顺序。 It sounds like it would give you some sorting capability, but it can only guarantee that the order in which you loop through keys is the same as the order you put them in the dictionary. 听起来它会为您提供一些排序功能,但只能保证循环浏览键的顺序与将其放入字典的顺序相同。

This should help you out though: 这应该可以帮助您:

od = OrderedDict()
for key in sorted(output):
    od[key] = output[key]

This will put all the keys of the original dict output in a list, sort it, and then stick each one in sorted order in the OrderedDict with the correct value. 这会将原始dict output所有键放在一个列表中,对其进行排序,然后以正确的值按OrderedDict中的排序顺序粘贴每个键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM