简体   繁体   中英

How to write dict in file with specific format and headers?

I have a dict:

dictionary1 = {100: {num: 1, age: 2, line: 3}}
dictionary2 = {90: {num: 1, age: 50, line: 4}}

I need to write this data into file line by line in the following format:

# HEADER 100 #
1 1 2 3
# HEADER 90 #
1 1 50 4

How to do that using Python?

for i in dictionary1:
  print('HEADER ',i)
  print('1 '+' '.join([str(x) for x in list(dictionary1[i].values())]))

You can change dictionary1 to dictionary2 for other values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM