'How to convert a dictionary value from a string to an int (Or how do I better write my code read it the value as an integer) in python
I am attempting to open a text file the following way:

However, the value is reading as a string as opposed to an int. How do I get the value from the text file to read as an int? For example a file reading CSCI 160:4 CSCI 289:3 EE 201:4 MATH 208:3 prints out as the dictionary {'CSCI 160': '4', 'CSCI 289': '3', 'EE 201': '4', 'MATH 208': '3'}
Solution 1:[1]
After you create ClassDict you can convert all values to integers, for example:
ClassDict = {k: int(v) for k, v in ClassDict.items()}
print(ClassDict)
Prints:
{'CSCI 160': 4, 'CSCI 289': 3, 'EE 201': 4, 'MATH 208': 3}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Andrej Kesely |
