Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

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

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
shellee1983
Asked:
shellee1983
  • 4
  • 4
1 Solution
 
aikimarkCommented:
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
 
shellee1983Author Commented:
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
 
aikimarkCommented:
is it safe to assume that the list is already in typ1 sorted order?
0
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
shellee1983Author Commented:
yes, but essentially the order really does not matter.
0
 
aikimarkCommented:
the order really does not matter
The order matters if you are selecting algorithms.

What file names do you need to produce?
0
 
shellee1983Author Commented:
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
 
aikimarkCommented:
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
 
shellee1983Author Commented:
Thank you so much! I am struggling converting from LUA :(
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now