Python - Import file into NamedTuple -


recently had question regarding data types.
since then, i've been trying use namedtuples (with more or less success).

my problem currently:
- how import lines file new tuples,
- how import values separated space/tab(/whatever) given part of tuple?

like:

monday  8:00    10:00   etr_28135   lh1n1522    computer science    1      tuesday 12:00   14:00   etr_28134   lh1n1544    geography ea    1   

first line should go tuple[0]. first data: tuple[0].day; second: tuple[0].start; ..and on.
, when new line starts (that's 2 tab (\t), start new tuple, tuple[1]).

i use separate data:

with open(filename) f:     line in f:         rawdata = line.strip().split('\t')   

and rest of logic still missing (the filling of tuples).

(i know. question, , recent 1 low-level ones. however, hope these others too. if feel it's not real question, simple question, etc etc, vote close. thank understanding.)

such database files called comma separated values though not separated commas. python has handy library called csv lets read such files

here modified example docs

csv.register_dialect('mycsv', delimiter='\t', quoting=csv.quote_none) open(filename, 'rb') f:     reader = csv.reader(f, 'mycsv') 

usually work 1 line @ time. if need whole file in tuple then:

t = tuple(reader) 

edit

if need access fields name use cvs.dictreader, don't know how works , not test here.

edit 2

looking @ namedtuples are, i'm bit outdated. there nice example on how namedtuple work csv module:

employeerecord = namedtuple('employeerecord', 'name, age, title, department, paygrade')  import csv line in csv.reader(open("employees.csv", "rb")):     emp = employeerecord._make(line)     print emp.name, emp.title 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -