JSON中的数据有我不需要的字母和数字,如何在Python中获得我需要的数据



我正在考虑从JSON文件中提取数据,但我需要的数据在数据之前和之后都有数字和字母。我想知道是否可以删除我不需要的不必要的数字和字母。以下是数据示例:

"most_common_aircraft":[{"planned_aircraft":"B738/L","dcount":4592},{"planned_aircraft":"H/B744/L","dcount":3639},{"planned_aircraft":"H/B77L/L","dcount":2579},{"planned_aircraft":"H/B772/L","dcount":1894},{"planned_aircraft":"H/B763/L","dcount":1661},{"planned_aircraft":"H/B748/L","dcount":1303},{"planned_aircraft":"B712/L","dcount":1289},{"planned_aircraft":"B739/L","dcount":1198},{"planned_aircraft":"H/B77W/L","dcount":978},{"planned_aircraft":"B738","dcount":957}]

"H/B77L/L,B752/L,A320/X,B738,";

我感兴趣的只是主要的4个字母/数字,例如;H/B77L/L";我只想要";B77L";,而不是";B752/L";我想要";B752";。数据非常混杂,所以有些在前面有一个字母,有些在后面,有些同时有两个字母,还有一些已经是我想要的正确格式。在使用Python从JSON文件中提取数据的过程中,有没有办法删除额外的字母?如果没有,会不会更好,因为我使用Pandas将它们全部提取到一个数据帧中,然后将其与另一个没有额外字母的具有正确序列的数据帧进行比较?

我已经设法找到了答案并解决了我的问题。我会把它放在这里,以便帮助其他可能有类似问题的人-

for entry in json_data['results']:
for value in entry['most_common_aircraft']:
for splitted_string in value['planned_aircraft'].split('/'):
if len(splitted_string) == 4:
value['planned_aircraft'] = splitted_string

最新更新