CSV to VCARD - Skip lines with specific pattern

57 views Asked by At

I have my customers exported as a CSV file.

Then its been converted to VCARD via Python script. But it also keeps empty entries, which I would like to be removed in the process.

I modified a script to pass the lines matched by RegEx.

To get it done I added following lines into the script:

        for row in source:
            if re.match("^\d+;;;;;;-1;nein;;;;;;;;;;.*",row):
                pass
            else:
def convert(somefile):
    #assuming file format : lastname,firstname,phonenumber,mail
    with codecs.open( somefile, 'r', encoding='cp1252' ) as source:
        for row in source:
            if re.match("^\d+;;;;;;-1;nein;;;;;;;;;;.*",row):
                pass
            else:
                reader = csv.reader( source, delimiter=';' ) #reader now holds the whole data like ['lastname', 'firstname', 'phonenumber', 'mail']
                allvcf = codecs.open('ALL.vcf', 'w', encoding='utf8')
                i = 0
                next(reader)
                for row in reader:

                    #write in the "ALL.vcf" file.
                    allvcf.write( 'BEGIN:VCARD' + "\n")
                    allvcf.write( 'VERSION:3.0' + "\n")
                    allvcf.write( 'N:' + row[5] + ';' + row[4] + "\n")
                    allvcf.write( 'FN:' + row[4] + ' ' + row[5] + "\n") #remember that lastname first
                    allvcf.write( 'ORG:' + "\n")
                    allvcf.write( 'TEL;type=HOME:' + row[12] + "\n")
                    allvcf.write( 'TEL;type=WORK:' + row[13] + "\n")
                    allvcf.write( 'TEL;type=CELL:' + row[14] + "\n")
                    allvcf.write( 'EMAIL;type=INTERNET;type=WORK;type=pref:' + row[15] + "\n")
                    allvcf.write( 'NOTE:' + row[1] + "\n")
                    allvcf.write( 'END:VCARD' + "\n")
                   # allvcf.write( "\n")

                    i += 1#counts

        allvcf.close()
        print (str(i) + " vcf cards generated")


def main(args):
    if len(args) != 2:
        print ( "Usage:")
        print ( args[0] + " filename")
        return

    convert(args[1])

if __name__ == '__main__':
    main(sys.argv)

I expected the RegEx in the for loop would sort out the matching lines. But that is not happening. My guess is due to encoding it never matches, but how can I skip those lines?

0

There are 0 answers