I have a large text file that reads like
Kyle 40 Greg 91 Reggie 58 How would I convert this to an array that looks like this
array = ([Kyle, 40], [Greg, 91], [Reggie, 58]) Thanks in advance.
I have a large text file that reads like
Kyle 40 Greg 91 Reggie 58 How would I convert this to an array that looks like this
array = ([Kyle, 40], [Greg, 91], [Reggie, 58]) Thanks in advance.
Assuming proper input:
array = [] with open('file.txt', 'r') as f: for line in f: name, value = line.split() value = int(value) array.append((name, value)) open should work unless you have it redefined somewhere else.Alternative to Manny's solution:
with open('file.txt', 'r') as f: myarray = [line.split() for line in f] for line in f is more idiomatic than for line in f.read()Output is in the form:
myarray = [['Kyle', '40'], ['Greg', '91'], ['Reggie', '58']] strip() is not needed as split() will ignore excess whitespace. For example, ' red\n\r\t blue \n'.split() gives ['red', 'blue'].myarray = tuple([line.split() for line in f])