nebffa
nebffa

Reputation: 1549

Importing .txt data in a particular format

this is a following sample of data

"TABITHA","OLLIE","JAIME","WINIFRED"

followed by a piece of code that correctly reads it in.

with open("names.txt", 'rb') as f:
    data = [map(str, line.split(',')) for line in f]

However, this reads it in as a list of lists. The list being only one list 'high',

e.g. [["TABITHA", "OLLIE", "JAIME", "WINIFRED"]]

and therefore might as well be just a list, not a list of lists. Is there a way to read it in as just a list? The journey of learning continues :- )

Upvotes: 0

Views: 83

Answers (4)

tsm
tsm

Reputation: 3658

If you want to be foolish and not use a CSV module:

sum([line.split(", ") for line in f], [])

Edit: What's the matter with my initial data then?

>>> f
['TABITHA, OLLIE, JAIME, WINIFRED', 'Spam, Eggs, Dead Parrots']
>>> sum([line.split(", ") for line in f], [])
['TABITHA', 'OLLIE', 'JAIME', 'WINIFRED', 'Spam', 'Eggs', 'Dead Parrots']

With a file:

$ cat temp.csv
foo, bar, baz
spam, eggs, quux
$ python
>>> f = open("temp.csv")
>>> sum([line.split(", ") for line in f], [])
['foo', 'bar', 'baz\n', 'spam', 'eggs', 'quux\n']

(Add in a map(str.strip, ...) to get rid of trailing whitespace)

Upvotes: 0

Levon
Levon

Reputation: 143047

Edit

Functional, "roll-your-own", code:

with open("names.txt", 'rb') as f:
    for line in f:
       line = line.strip()
       line = [w.replace('"','') for w in line.split(',')]
       print line

creates the list line:

['TABITHA', 'OLLIE', 'JAIME', 'WINIFRED']

Upvotes: 0

Mark Tolonen
Mark Tolonen

Reputation: 177575

Use the "batteries included" csv (comma-separated values) module. Despite the name, it can also be configured for other separators and has a number of other options as well.

import csv
with open('names.txt','rb') as f:
    for line in csv.reader(f):
        print line

Output:

['TABITHA', 'OLLIE', 'JAIME', 'WINIFRED']

Upvotes: 3

asthasr
asthasr

Reputation: 9397

The best way to read something like this is to use the csv module. This will correctly deal with embedded commas that might appear in the quoted values.

Upvotes: 0

Related Questions