Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s = """
- A B C
- 1 0.1 300
- 2 0.2 400 (11 - this text is part of C in row 2 but needs to be ignored / removed)
- 3 0.9 600"""
- print(my_dict)
- {'1': {'B': '0.1', 'C': '300'}, '2': {'B': '0.2', 'C': '400'}, '3': {'B': '0.9', 'C': '600'}}
- s = """
- A B C
- 1 0.1 300
- 2 0.2 400 (11 - this text is part of C in row 2 but needs to be ignored / removed)
- 3 0.9 600"""
- #Get the columns and assign them to a variable.
- columns = s.lstrip().splitlines()[0] #Print the first line of the string
- dct = {}
- rows = s.lstrip().splitlines()
- for data in rows[1:]:
- row = data.split()
- dct[row[0]] = dict(zip(columns[1:], row[1:]))
- print(dct)
- {'1': {' ': '0.1', 'B': '300'}, '2': {' ': '(11', 'B': '400', 'C': '-'}, '3': {' ': '0.9', 'B': '600'}}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement