Search code examples
pythonpython-3.xinputyamlpyyaml

how can i get yaml format with input in python ??(without using pyyaml library)


I want to get data from input that is in yaml format.The data includes user information and music albums information that each user has purchased.Input information is as follows:

2 # this line specify the number of users
- name: user1
  age: 18
  city: city1
  albums:
    - album1
    - album2
    - album3
- name: user2
  age: 20
  city: city2
  albums:
    - album2
    - album1
    - alubm3
3 # this line specify the number of albums 
- name: album1
  singer: singer1
  genre: classic
  tracks: 10
- name: album2
  singer: singer2
  genre: pop
  tracks: 22
- name: album3
  singer: singer3
  genre: pop
  tracks: 14

I wrote the following code for this

num_user = int(input())

users_data = {}
albums_data = {}

for i in range(num_user):
    name, age, city = input().split()[-1], input().split()[-1], input().split()[-1]
    input()
    albums=[]
    next_line = input()
    while next_line.split()[0]=='-' and len(next_line)-len(next_line.lstrip(' '))==4:
        albums.append(next_line.split()[-1])
        next_line = input()
    if len(next_line.split()) < 2:
        num_albums = int(next_line)
    users_data[name]=[age, city, albums]
for i in range(num_albums):
    name, singer, genre, tracks = input().split()[-1],input().split()[-1],\
            input().split()[-1], input().split()[-1]
    albums_data[name]=[singer, genre, tracks]

Everything is in order until the number of users exceeds one person and I have trouble storing the second user information in the dictionary and all the moving information is stored. I want this:

{'user1': ['18', 'city1', ['album1', 'album2', 'album3']], 'user2': ['20', 'city2', ['album2', 'album1', 'alubm3']]} {'album1': ['singer1', 'classic', '10'], 'album2': ['beeptunes', 'pop', '22'], 'tekunbede': ['beeptunes', 'pop', '14']}

but get this:

{'user1': ['18', 'city1', ['album1', 'album2', 'album3']], '20': ['city2', 'albums:', ['album1', 'alubm3']]} {'album1': ['singer1', 'classic', '10'], 'album2': ['beeptunes', 'pop', '22'], 'tekunbede': ['beeptunes', 'pop', '14']}

Solution

  • The issue seems to be that once you have processed the last album for the first user you are then calling input() again which is getting the name. Decoupling the input from the processing will help to fix the issue so have a look at creating a function to process a name once its been detected.

    so try:

    1. read the input
    2. work out what do based on the input
    3. process the read data
    num_user = int(input())
    
    users_data = {}
    albums_data = {}
    
    for i in range(num_user):
        name, age, city = input().split()[-1], input().split()[-1], input().split()[-1]
        input()
        albums=[]
        next_line = input()
        while next_line.split()[0]=='-' and len(next_line)-len(next_line.lstrip(' '))==4:
            albums.append(next_line.split()[-1])
            next_line = input() # This is the line with the issue
        if len(next_line.split()) < 2:
            num_albums = int(next_line)
        users_data[name]=[age, city, albums]
    for i in range(num_albums):
        name, singer, genre, tracks = input().split()[-1],input().split()[-1],\
                input().split()[-1], input().split()[-1]
        albums_data[name]=[singer, genre, tracks]