Skip to content

Instantly share code, notes, and snippets.

@iainnash
Created January 9, 2022 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save iainnash/bc977015d34f844242b6d995a9efd004 to your computer and use it in GitHub Desktop.
Save iainnash/bc977015d34f844242b6d995a9efd004 to your computer and use it in GitHub Desktop.
sort ipfs files output by filename into json file
import re
import glob
import json
INPUT_FILE = 'files-test.txt'
OUTPUT_FILE = 'output-hashes.json'
data = []
with open(INPUT_FILE, 'r') as f:
data = f.readlines()
hashes_by_id = {}
for indx, file in enumerate(data):
print(file, indx)
[hash, _, fnameraw] = re.split(r'[ ]+', file)
[fname, ext] = fnameraw.strip().split('.')
hashes_by_id[fname] = hash
output_hashes = []
for i in range(len(data)):
key = str(i+1)
try:
output_hashes.append(hashes_by_id[key])
except:
print('cannot find key ', key)
with open(OUTPUT_FILE, 'w') as f:
json.dump(output_hashes, f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment