I'm having some trouble converting type 'str' to numbers. I use a separate text-file containing the following numbers 1, 2, 3, 4, 5, 6
and then I import these numbers into python and save them as a list. However, by doing this, I get a list of strings as follows: ['1', '2', '3', '4', '5', '6']
. I want to convert this list of strings so the list represents numbers, i.e. the output should be [1, 2, 3, 4, 5, 6]
.
My code is:
def imported_numbers(filename):with open(filename, 'r') as f: contents = f.read().splitlines() print(contents)
imported_numbers('sample.txt')
Is there a specific command to do this?