1

Is there a way to inject the header array into the header vector in a more efficient way without using any packages such as csv, pandas, etc...?

data = []
b = 1
with open(datafile, "rb") as f:
    for line in f:
        if b:
            header=line.strip('\n').split(',')
            b = 0
            continue
        entries=line.strip('\n').split(',')
        data.append(dict(zip(header,entries)))
        #print data
return data
4
  • 2
    That looks like csv.DictReader minus a lot of edge-case handling. Commented Mar 10, 2014 at 13:04
  • I can imagine situations where you might not want or might not be able to use pandas; but what's the reason to avoid csv? Commented Mar 10, 2014 at 13:48
  • No reason. I wanted to write it like this for educational purposes. Commented Mar 10, 2014 at 13:54
  • If you're going to write one for educational purposes, you should make it non-trivial. Handling quoting correctly would be a good feature to add. Commented Mar 10, 2014 at 14:24

2 Answers 2

1

If you don't need to go through the same file twice, yielding values is usually better than returning a list.

with open(datafile, "rb") as f:
    header = next(f).strip('\n').split(',')
    for line in f:
        entry=line.strip('\n').split(',')
        yield dict(zip(header,entry))
Sign up to request clarification or add additional context in comments.

Comments

0

I just rewrite your code using list comprehension. I don't know it is fast or slow. And I think it is not easy to read. Use it just educational purposes.

datafile = "hoge.csv"
l = [line.strip('\n').split(',') for line in open(datafile, "rb")]
data = [dict(zip(l[0],r)) for r in l[1:]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.