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))       
 
    
    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']]
 
    
    ... or even shorter than presented in the accepted answer:
array = [(name, int(i)) for name,i in open(file)]
 
    
    Open the file, read in each line, strip the newline character off the end, split it in the space character, then convert the second value into an int:
array = [(name, int(number)) for name, number in
         (line.strip().split() for line in open('textfile'))]
