Splitting VCF files using Python

I recently did a fresh install of Ubuntu 11.10. I forgot to export my contacts from Evolution and was horrified to learn these are not stored in a flat file and the database is not compatible between versions (great work Evolution devs).

Some poking around on the internet led me to a Perl file to that was able to get the data. Then some Python work let me format it correctly as a VCARD (.VCF) file. But when I tried to import it I didn’t get all my contacts. More sleuthing led me to realize that only the first 75 were being imported. I wrote this short Python script to break up my 190-contact VCARD file into parts that had no more than 75 entries. I hope it will help you out too!

#split vcf files

working_dir = '/home/mike/compile/'
input_file = 'final.vcf'
output_seed = 'contacts-part-'
vcards_per_file = 75

with open(working_dir + input_file,'r') as f:
    count = 0
    output_count = 1
    results = []
    for line in f:
        if ("BEGIN:VCARD" in line):
            count += 1
        if (count <= vcards_per_file):
            results.append(line)
        else:
            #output file with stored values
            with open(working_dir + output_seed + str(output_count) + '.vcf','w') as oFile:
                for item in results:
                    oFile.write(item)

            #increment outputfile count
            output_count += 1

            #clear results list and append last read line
            del results[:]
            results.append(line)

            #set counter back to 1
            count = 1
            

    #write the last set of results to a file
    with open(working_dir + output_seed + str(output_count) + '.vcf','w') as oFile:
        for item in results:
            oFile.write(item)

Python parallel port control

 

Looking for a really easy way to control your project from a computer? If you have a parallel port which isn’t used you’re in luck. Python has a module that makes it easy to toggle the pins on the parallel port

First install the pyParallel module. It’s in the Ubuntu repositories:

sudo apt-get install python-parallel

To use the module just import it, instantiate an object, then write or read from that object.

import parallel
parPort = parallel.Parallel()
parPort.setData(0x01)

Now, this threw a permission error for me. But a bit of searching led me to find that you need to remove the lp module and insert the ppdev module:

sudo rmmod lp
sudo modprobe ppdev

This module will load again next time you reboot. Consider blacklisting it if you are using automated Python scripts that need parallel port access.

That’s it! Don’t you love Python? Of course there are some additional functions availalbe for this module so check the documentation to see what else can be done.