Skip to content
Advertisement

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement