1

I have to read from file into a 2D list. The file looks like:

Jack 32
Perry 12
Tim 14

etc.

When I print the list it looks like:

['Jack 32', 'Perry 12', 'Tim 14']

But I need it to look like:

['Jack', '32', 'Perry', '12', 'Tim', '14']

So far my code looks like this:

with open('filelocation', 'r') as f:
    content = f.readlines()
    content = [x.strip('\n') for x in content]
    print(content)
kdopen
  • 8,032
  • 7
  • 44
  • 52
G. Striela
  • 121
  • 2
  • 2
    Check this: http://www.tutorialspoint.com/python/string_split.htm – Nic007 May 14 '15 at 14:19
  • possible duplicate of [How to split a string into two integers in python](http://stackoverflow.com/questions/6429638/how-to-split-a-string-into-two-integers-in-python) – felipsmartins May 14 '15 at 14:20
  • your x.strip('\n') is splitting by lines, you need to split by lines or blank spaces – Jblasco May 14 '15 at 14:22

8 Answers8

3

Can't you just split the whole file on whitespace?

>>> with open('filelocation') as f:
        print(f.read().split())

['Jack', '32', 'Perry', '12', 'Tim', '14']

No point creating structure only to flatten it away.

Stefan Pochmann
  • 27,593
  • 8
  • 44
  • 107
1

Split the string for each element in ['Jack 32', 'Perry 12', 'Tim 14'] using space as a separator (default).

Iterate through the resulting list to flatten it out.

mylist = ['Jack 32', 'Perry 12', 'Tim 14']
my_new_list = []
for l in mylist:
    my_new_list.extend(l.split())

Or, you could use a generator

Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85
1

You should just be able to read as a string and split on whitespace.

with open('filelocation') as f:
    split_up = f.read().split()
jwilner
  • 6,348
  • 6
  • 35
  • 47
1

Try like this:

my_list = []
with open('your_file') as f:
    for x in f:
        my_list += x.strip().split()
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
0

I think if you took the contents of the list you get back and split it on white space that will give you what you're looking for.

https://docs.python.org/2/library/stdtypes.html#str.split

HoppyScoot
  • 73
  • 3
0

There might be a simple method than this

 In [46]: a=['Jack 32', 'Perry 12', 'Tim 14']

In [47]: b=[i.split() for i in a ]
In [49]: b
Out[49]: [['Jack', '32'], ['Perry', '12'], ['Tim', '14']]

In [50]: [item for sublist in b for item in sublist]
Out[50]: ['Jack', '32', 'Perry', '12', 'Tim', '14']
Ajay
  • 5,267
  • 2
  • 23
  • 30
0

You can chain the elements with itertools.chain after splitting each line:

from itertools import chain
with open('filepath') as f:
    content = list(chain.from_iterable(x.split() for x in f))
    print(content)
['Jack', '32', 'Perry', '12', 'Tim', '14']

You don't need to call .readlines, you can iterate over the file object f. line.strip("\n") just strips the newlines, you don't actually split. If you call split on each line you will end up with a list of lists like [['Jack', '32'], ['Perry', '12'], ['Tim', '14']], chain.from_iterable chains the elements from each sublist into a single list.

.readlines reads all the lines into memory at once which for a small file won't matter but if you have large files or restricted memory you should avoid calling .read or .readlines.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
0

Why not stream the file into a string with spaces between the new lines, then split by the space?

All you want to do is split by space anyway.

I know this isn't the "sexiest" answer, but probably the most straight forward for your use case.

jason m
  • 6,519
  • 20
  • 69
  • 122