I have done analysis around stack overflow for an answer to this but it feels a bit specific, happy to go to your links if you suggest them.
A. I've got a dictionary holding a bunch of dictionaries in this format:
Key : {Nested_Key: Nested_Value}
Key = Document ID, Data Type is String
Nested Key = Token ID, Data Type is Integer
Nested Value = Count of Token ID, Data Type is Integer
Example:
d = { '441' : {201:1, 220:1, 232:1, ..., 23231:4},
'4452' : {203:43, 2202: 45} }
B: I'd like to be able to output them into a text file in this format:
document_(key), nested_key:nested value, nested_key:nested value \n
document_(key+1), nested_key:nested value, nested_key: nested value \n
I'm able to get fairly close to what I want using this code:
with open("text.txt", "w") as f:
for key, token in dict.items():
f.write('doc%s,%s\n' % (key, token))
But as you would expect it outputs the nested key and nested value in its dictionary
doc_441,{201: 1, 220: 1, 232: 1, 240: 1, 241: 1, 242: 1, 243: 1, 245: 1, 246: 1, 250: 1, 255: 1, 260: 1, 271: 1, 493: 1, 494: 1, 540: 1, 608: 1, 609: 1, 610: 1, 611: 1, 612: 1, 613: 1, 614: 1, 835: 1, 836: 1, 965: 1, 966: 1, 967: 1, 986: 1, 1291: 1, 1292: 1, 1734: 1, 1735: 1, 1736: 1, 1748: 1, 1749: 1, 1762: 1, 1763: 1, 1818: 1, 1819: 1, 1820: 1, 1821: 1, 1822: 1, 1875: 1, 1881: 1, 1882: 1, 1883: 1, 1890: 1, 1891: 1, 1941: 1, 1947: 1, 1948: 1}
doc_577,{201: 1, 205: 1, 217: 1, 232: 1, 233: 1, 235: 1, 236: 1, 237: 1, 238: 1, 241: 1, 242: 1, 243: 1, 244: 1, 245: 1, 246: 1, 247: 1, 248: 1, 249: 1, 250: 1, 251: 1, 280: 1, 448: 1, 493: 1, 494: 1, 537: 1, 540: 1, 571: 1, 572: 1, 573: 1, 574: 1, 575: 1, 669: 1, 670: 1, 671: 1, 672: 1, 673: 1, 674: 1, 675: 1, 690: 1, 731: 1, 732: 1, 733: 1, 734: 1, 735: 1, 736: 1, 770: 1, 771: 1, 772: 1, 773: 1, 777: 1, 947: 1, 948: 1, 949: 1, 950: 1}
I've tried some ridiculously convoluted code to try and cajole the code into what I want but this one only outputs the last line in the nested dictionary's nested key: nested token
combined_file_content = ""
# For Key, Token pair in dict
for key, token in dict.items():
for nest_key, nest_token in iter(token.items()):
file_content = ""
# This side works
int_id = ""
# Set int_id = doc_(key).txt,
int_id = "doc_" + '(' + key + ')' + '.txt' + ','
# Then for token dictionary,
# For nested key, nested token pair in token dictionary
# Set int_value = key:value
int_value = ""
nested_key = nest_key
nested_token = nest_token
int_value += (str(nested_key) + ":" + str(nested_token))
#print(str(nested_key) + ":" + str(nested_token))
combined_nest = int_id + int_value + "\n"
file_content += combined_nest
combined_file_content += file_content
Output: docresume_(441).txt,4334:1\nresume_(577).txt,4344:1\ndoc_(220).txt,4305:1\n
Any help would be much appreciated. Thank you.
python yourcode.py > yourfile.txt- drkostas