Instantly share code, notes, and snippets.

@sakp /fix-img-key.py Secret
Created Sep 10, 2014

Embed
What would you like to do?
#!/usr/bin/python
# fix-img-key.py
import sys
import glob
import re
def format_it(inp):
outp = {}
for i,k in zip(inp[0::2], inp[1::2]):
outp[i] = k;
return outp
def merge_it(inp):
out = []
for rec_key, rec_value in inp.items():
out.append(rec_key+"\t"+rec_value)
return "\t".join(out)
for datarec_file in glob.glob("./invt/*/data.rec"):
read_data = ""
f = open(datarec_file, 'r')
read_data = f.read()
f.closed
formatted_splited = format_it(read_data.strip().split("\t"));
maxkey = 0;
for rec_key, rec_value in formatted_splited.items():
result = re.match( r'^medname([0-9]+)', rec_key, re.M|re.I)
if result:
keynum = int(result.group(1));
medname = 'medname'+str(keynum)
medk = 'medk'+str(keynum)
if re.match( r'^attr', formatted_splited[medname], re.M|re.I):
if maxkey < keynum:
maxkey = keynum
formatted_splited[medk] = "swatch"
if maxkey > 0:
print("processing .. ("+str(maxkey)+")", formatted_splited['ref'])
formatted_splited['medtot'] = str(maxkey)
f = open (datarec_file, 'w')
f.write(merge_it(formatted_splited)+"\n")
f.closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment