1

I am attempting to parse a file. Currently, I have this file:

word1 52345325
word2 12312314
word3 7654756
word4 421342342

I am attempting to store word1 word2 word3 and word4 into an array and the numbers adjacent to those words into another array.

So if i say a[0] i should get word1, and if I say b[0] i should get 52345325 and so on.

I was thinking about making a key-valued pair dictionary object but that may be a little complex at this point as I am just getting into python.

I currently am doing this but of course, it ain't working :P

def csvStringParser():
    a = {}
    b = {}
    i = 0
    f = open('/Users/settingj/Desktop/NOxMultiplier.csv')
    for line in f.readlines():
    reader = csv.reader(line.split('\t'), delimiter='\t')
        for row in reader:
            #print '\t'.join(row)                                                                                                                                                                                                                                             
            #print i                                                                                                                                                                                                                                                          
            a[i] = '\t'.join(row)
            b[i] = '\t'.join(row)
            print a[i]
            print b[i]
            i+=1

This is honestly my first hour of using python. I could easily do this in C++ but I'm am currently just trying to learn python to understand it's greater benefits/simplicity over c++.

0

3 Answers 3

2
import csv

a = {}
with open('/Users/settingj/Desktop/NOxMultiplier.csv') as f:
    reader = csv.reader(f, delimiter='\t')
    for row in reader:
        a[row[0]] = row[1]
print a

For two arrays:

a = []
b = []
with open('/Users/settingj/Desktop/NOxMultiplier.csv') as f:
    reader = csv.reader(f, delimiter='\t')
    for row in reader:
        a.append(row[0])
        b.append(row[1])
print a
print b

of even just similar solution with the zip:

with open('/Users/settingj/Desktop/NOxMultiplier.csv') as f:
    a, b = zip(*csv.reader(f, delimiter='\t'))
print a
print b
Sign up to request clarification or add additional context in comments.

3 Comments

haha amazing... dam easy... You get a vote up for copy and paste code, but I am still wondering, how would I create 2 arrays instead of one that are mapped to one another like how I explained in my questions?
In the end, I feel like the first answer is my final desired goal, but I am not able to use it..
Added two arrays example.
2

Ok, in fact, there is only one line of code:

a, b = zip(*(map(lambda x: x.rstrip('\n\r').split('\t'), open('file.csv').readlines())))

Some links:

1 Comment

your answer is perfect for a dictionary solution like how I specified in the question, and it's so compact :) I like it.
1

Here's my implementation that does what you describe with the input you describe: #!/usr/bin/python

def csvStringParser(filename):
    a = []
    b = []
    f = open(filename)
    for line in f.readlines():
        tok = line.split()
        a.append(tok[0])
        b.append(tok[1])
    print a
    print b

if __name__=="__main__":
    csvStringParser('temp.txt')

You could use other separators in split() such as split('\t') if you use tab delimiters, but you probably don't need the csv package for what you describe.

2 Comments

this is basically the same as the accepted answer but you get a vote up since it works :)
Yeah, I saw the accepted answer after I posted mine. Glad you got a working code sample.

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.