Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
understand function
#1
I did not understand how this function works properly? can anyone help me to understand it and to test it?. i work with csv file

# Convert each line into a dictionary
dict(zip(cols, [ a.strip() for a in csv.reader([line]).next()]))
Reply
#2
[a.strip()for a in csv.reader([line]).next()]
this will create a list.

zip(cols, [ a.strip() for a in csv.reader([line]).next()])
this will create list of 2-element tuples, assuming cols is also iterable

dict(zip(cols, [ a.strip() for a in csv.reader([line]).next()]))
this will create dict out of the tuples - first element in the tuple used as a key, second element - as item.

All that said it could be written way better. Could you post your entire code - i.e. where cols and line comes from
Reply
#3
I have two files, from which I need to get ip and port information or more generally find what is equal or what is different using mrjob. The following code is a draft of what I'm trying to do.
from mrjob.job import MRJob
import csv
cols='ipv,vvv.vvv.vvv'.split(',') 

def parse_line(line_str):
    return dict(zip(cols,[a.strip() for a in csv.reader([line_str]).next()]))

def parse_ip_address(ip_str):
    ip=ip_str;
    return ip

def parse_port(p_str):
    port=p_str;
    return (port)

class Myclass(MRJob):
    def mapper(self, _, line):
        #parse the line from current CVS form
        p1=parse_line(line)
        #get ip
        ip_address=parse_ip_address(p1['vvv.vvv.vvv'])
        #get port
        port=parse_port(p1['ipv'])
        #yield a key and a value
        yield port,ip_address

    #reducer function
    def reducer(self, key, values):
        yield key

if __name__ == '__main__':
    Myclass.run()
Reply
#4
I have the python code for comparing the two files, but I did not understand how to use it on mrjob.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Don't Understand Recursive Function muzikman 9 3,576 Dec-03-2020, 05:10 PM
Last Post: muzikman
  Need to understand the way enumerate() function works abhishekambastha 2 2,441 Sep-16-2018, 08:21 PM
Last Post: snippsat
  i don't understand how to use a variable outside of a function? Rius2 6 5,223 Oct-04-2017, 06:31 PM
Last Post: wavic
  Can't understand the output of this recursion function bigmit37 5 3,904 Apr-04-2017, 11:15 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020