Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.81 KB | None | 0 0
  1. s = """
  2. A B C
  3. 1 0.1 300
  4. 2 0.2 400 (11 - this text is part of C in row 2 but needs to be ignored / removed)
  5. 3 0.9 600"""
  6.  
  7. print(my_dict)
  8. {'1': {'B': '0.1', 'C': '300'}, '2': {'B': '0.2', 'C': '400'}, '3': {'B': '0.9', 'C': '600'}}
  9.  
  10. s = """
  11. A B C
  12. 1 0.1 300
  13. 2 0.2 400 (11 - this text is part of C in row 2 but needs to be ignored / removed)
  14. 3 0.9 600"""
  15.  
  16. #Get the columns and assign them to a variable.
  17. columns = s.lstrip().splitlines()[0] #Print the first line of the string
  18.  
  19. dct = {}
  20.  
  21. rows = s.lstrip().splitlines()
  22.  
  23. for data in rows[1:]:
  24. row = data.split()
  25. dct[row[0]] = dict(zip(columns[1:], row[1:]))
  26.  
  27. print(dct)
  28.  
  29. {'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