1

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.

joaquin
  • 82,968
  • 29
  • 138
  • 152
Keith Michael
  • 55
  • 1
  • 3
  • As @Cosmologicon mentions below, you probably want your array to be a list of tuples, not a tuple of lists. Read up on the difference here: http://docs.python.org/tutorial/datastructures.html, http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples-in-python – Eric Wilson Jul 12 '11 at 15:49

4 Answers4

6

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))       
Manny D
  • 20,310
  • 2
  • 29
  • 31
  • This doesn't work for me... I'm using Python 2.7 if that makes a difference. – Keith Michael Jul 12 '11 at 15:44
  • 1
    It's not clear from the OP, but you may want to convert the second item to an int. Also the OP's "array" is a tuple of lists, whereas yours is a list of tuples. List of tuples is probably the better way to do it. – Cosmologicon Jul 12 '11 at 15:45
  • Python claims I have invalid syntax on line 3, "with open('data.txt', 'r') as f: " . It specifically points to the word "open". – Keith Michael Jul 12 '11 at 15:47
  • @Cosmo, I assumed he meant list of tuples, OP, please clarify otherwise. Also @Keith, `open` should work unless you have it redefined somewhere else. – Manny D Jul 12 '11 at 15:49
  • Open just isn't working at all... I tried putting it in a different program and it again claimed I had a syntax error. – Keith Michael Jul 12 '11 at 15:52
  • @Keith, can you verify your syntax? Also post the line you're trying, if possible. – Manny D Jul 12 '11 at 15:55
  • @Steve, Hm, haven't used this method before. Good to know. +1 to you. – Manny D Jul 12 '11 at 16:04
  • Just closed out of the program and opened it again... now it works. Thanks so much for your patience and answer. – Keith Michael Jul 12 '11 at 16:07
  • @Keith: My current code does this for you. As you can see, you're appending `(name, value)` to array which is your tuple. – Manny D Jul 12 '11 at 16:10
  • @Keith: To do that is left as an exercise to the reader. j/k, use `[name, value]` in the append call instead of `(name, value)`. – Manny D Jul 12 '11 at 16:12
1

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']]
joaquin
  • 82,968
  • 29
  • 138
  • 152
  • `strip()` is not needed as `split()` will ignore excess whitespace. For example, `' red\n\r\t blue \n'.split()` gives `['red', 'blue']`. – Steven Rumbalski Jul 12 '11 at 15:54
  • @Keith Michael: If you really need a tuple of lists then change the second line to `myarray = tuple([line.split() for line in f])` – Steven Rumbalski Jul 12 '11 at 16:04
1

... or even shorter than presented in the accepted answer:

array = [(name, int(i)) for name,i in open(file)]
phynfo
  • 4,830
  • 1
  • 25
  • 38
0

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'))]
Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65