Home > Software design >  Getting first key from json file and split that key
Getting first key from json file and split that key

Time:06-13

Is there an easier way to grab the first key of a dictionary when parsing json files? I want to grab the first key: test.txt.zip.partaa to then split the string to only have test.txt.zip

I have a json file I am parsing looking as follows:

{
    "test.txt.zip.partaa": "558ebe6ce97b31020e87c144a8d0c16e1b735d55bbdeb6b95f0fbfb89db855d0", 
    "test.txt.zip.partab": "edc60ff4334534570c61f13325267c88d2e47b668747f4736b64a50c145b73fd", 
    "test.txt.zip.partac": "2e3d183f16ccc360750cfdb1bd06405be0e1145706f3ee72ae94f25b59e6a993"
}
output_filename = os.path.splitext(list(metadata.keys())[0])[0]
print(f"output_filename: {output_filename}")
# output: output_filename: test.txt.zip

CodePudding user response:

Like this? Only get the first value of the iterator through next()

first_key = next(iter(metadata))
print(os.path.splitext(first_key)[0])
  • Related