Solved

Python CSV help - matching from 1 field, outputs information from another field

Posted on 2014-10-03
8
219 Views
Last Modified: 2014-10-06
Right now I am working with a csv file that I am basically trying to use conditions from 1 column to out put the information from another column. The csv looks like this:

typ1,typ2,type3
aaa,unneed=trap,121
aaa,unneed=strap,222
aaa,unneed=cap,212
bbb,unneed=lock,112
bbb,unneed=stock,346
bbb,unneed=rock,333
ccc,unneed=vent,456
ccc,unneed=bent,444
ccc,unneed=sent,478

What I would like to do is to basically filter based on the 1st field, and have the output be from field 2.  This is the code I have so far:

for line in csv_file:
    altl = line['typ1']
    #cOut = altl + ', ' + line['typ2'] + '\n'
    cn = line['typ2'].split("=")[1] + '\n'
    #the next 3 lines were to test the output
   # if altl == 'aaa':
        #print cn
      #print 'this needs to be an array:' + '\n' + cn

The next portion of the code is where I get stuck. I know how to do this in LUA, but I really really need to get better at python. In LUA, I would loop through, something like this

i = 1,#endofsameitems

while i == altl do
stuff with cn
 end

Essentially I need the output to be:

print 'elements belonging to' + "'" altl "'" + ' :' + '\n' + cn

so that it looks something like this.

elements belonging to 'aaa':
trap
strap
cap

How exactly can I go about that?
0
Comment
Question by:shellee1983
  • 4
  • 4
8 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 40361439
Running this script:
import csv

def main(parmT1):
    with open("C:\Users\AikiMark\Downloads\Q_28531163.csv", "r") as f:
        csvdata=csv.DictReader(f)
        print "Records belonging to '" + parmT1 + "' :"
        for rec in csvdata:
            if rec["typ1"] == parmT1:
                print rec["typ2"].split("=")[1]

if __name__ == '__main__':
    main("bbb")

Open in new window

against your sample data, produces the following output:
Records belonging to 'bbb' :
lock
stock
rock

Open in new window

0
 

Author Comment

by:shellee1983
ID: 40361805
Hi, Thanks for the awesome help, but I am still stuck. I see that you used the actual information in the csv to filter it but i need to be able to create a loop that runs through the whole file.

So for instance, say that I want it to write to a file. SOmething like this:

varToFilter = rec["typ1"]
k = "Records belonging to '" + parmT1 + "' :"
        while true:
		i = 0
		varToFilter [i] == varToFilter[i] 
		writeOut(k)
		if varToFilter [i] =! rvarToFilter [i]
		pass_

Open in new window


So essentially, there would be 3 files after the csv is run as in

this would be written to a file:
Records belonging to 'bbb' :
lock
stock
rock

this would be written to another file:
Records belonging to 'aaa' :
trap
strap
cap

and this would be written to a third file:
Records belonging to 'aaa' :
trap
strap
cap

how would I got about doing something like that?

Also, where is a good resource to find this kind of information without asking?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40361816
is it safe to assume that the list is already in typ1 sorted order?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:shellee1983
ID: 40364036
yes, but essentially the order really does not matter.
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40364042
the order really does not matter
The order matters if you are selecting algorithms.

What file names do you need to produce?
0
 

Author Comment

by:shellee1983
ID: 40364287
The order matters if you are selecting algorithms.
I see what you are saying here, and yes they would be put into order first. The names of the files can be the field typ1.
0
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 40364517
Then the code will resemble this:
import csv

def main():
    with open("C:\Users\AikiMark\Downloads\Q_28531163.csv", "r") as f:
        csvdata=csv.DictReader(f)
        prevKey=None
        for rec in csvdata:
            if rec["typ1"] == prevKey:
                o.write( rec["typ2"].split("=")[1]+"\n")
            else:
                try:
                    o.close
                except:
                    pass
                prevKey = rec["typ1"]
                o=open('C:\\Users\\AikiMark\\Downloads\\' + prevKey + '.txt', 'w')
                o.write("Records belonging to '" + prevKey + "' :"+"\n")
                o.write( rec["typ2"].split("=")[1]+"\n")

if __name__ == '__main__':
    main()

Open in new window

0
 

Author Closing Comment

by:shellee1983
ID: 40364849
Thank you so much! I am struggling converting from LUA :(
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now