Monday, August 25, 2014

Python Read & Write Csv Files


import csv
from random import randint

class CsvObj:
    def __init__(self):
        self.kset = []
        self.vset = []
    def setheader(self, h):
        self.kset = h
    def addvalue(self, v):
        self.vset.append(v)
    def prettyprint(self):
        for obj in self.vset:
            pos = 0
            print '-----------------------------------------------------'
            for key in self.kset:
                print key, ': ', str(obj[pos])
                pos = pos + 1

    def tocsv(self, name):
        with open(name, 'w') as fp:
            a = csv.writer(fp, delimiter=',')
            data = []
            data.append(self.kset)
            for row in self.vset:
                row[0] = '%s , " , %s' % (str(row[0]), str(row[1]))
                data.append(row)
            a.writerows(data)
        return True
    

csvobj = CsvObj()
with open('countrylist.csv', 'rb') as f:
    reader = csv.reader(f)
    pos = 0
    for row in reader:
        if pos == 0:
            csvobj.setheader(row)                
        else:
            csvobj.addvalue(row)
        pos = pos + 1

print csvobj.prettyprint()
csvname = 'csv%s.csv' % str(randint(0, 99999))
if csvobj.tocsv(csvname) == True:
    print 'Csv %s created' % (csvname)

Output would be like this:

-----------------------------------------------------
Order :  1
Name :  Albania
Name :  Republic of Albania
Type :  Independent State
Sub Type :  
Sovereignty :  
Capital :  Tirana
Currency Code :  ALL
Currency Name :  Lek
Telephone Code :  +355
Letter Code :  AL
Letter Code :  ALB
Number :  008
Country Code TLD :  .al
-----------------------------------------------------
Order :  2
Name :  Algeria
Name :  People's Democratic Republic of Algeria
Type :  Independent State
Sub Type :  
Sovereignty :  
Capital :  Algiers
Currency Code :  DZD
Currency Name :  Dinar
Telephone Code :  +213
Letter Code :  DZ
Letter Code :  DZA
Number :  012
Country Code TLD :  .dz
-----------------------------------------------------
Order :  3
Name :  Andorra
Name :  Principality of Andorra
Type :  Independent State
Sub Type :  
Sovereignty :  
Capital :  Andorra la Vella
Currency Code :  EUR
Currency Name :  Euro
Telephone Code :  +376
Letter Code :  AD
Letter Code :  AND
Number :  020
Country Code TLD :  .ad
-----------------------------------------------------
Order :  4
Name :  Angola
Name :  Republic of Angola
Type :  Independent State
Sub Type :  
Sovereignty :  
Capital :  Luanda
Currency Code :  AOA
Currency Name :  Kwanza
Telephone Code :  +244
Letter Code :  AO
Letter Code :  AGO
Number :  024
Country Code TLD :  .ao
-----------------------------------------------------
Order :  5
Name :  Antigua and Barbuda
Name :  
Type :  Independent State
Sub Type :  
Sovereignty :  
Capital :  Saint John's
Currency Code :  XCD
Currency Name :  Dollar
Telephone Code :  +1-268
Letter Code :  AG
Letter Code :  ATG
Number :  028
Country Code TLD :  .ag
-----------------------------------------------------
Order :  6
Name :  Argentina
Name :  Argentine Republic
Type :  Independent State
Sub Type :  
Sovereignty :  
Capital :  Buenos Aires
Currency Code :  ARS
Currency Name :  Peso
Telephone Code :  +54
Letter Code :  AR
Letter Code :  ARG
Number :  032
Country Code TLD :  .ar
-----------------------------------------------------
Order :  7
Name :  Armenia
Name :  Republic of Armenia
Type :  Independent State
Sub Type :  
Sovereignty :  
Capital :  Yerevan
Currency Code :  AMD
Currency Name :  Dram
Telephone Code :  +374
Letter Code :  AM
Letter Code :  ARM
Number :  051
Country Code TLD :  .am
-----------------------------------------------------
Order :  8
Name :  Australia
Name :  Commonwealth of Australia
Type :  Independent State
Sub Type :  
Sovereignty :  
Capital :  Canberra
Currency Code :  AUD
Currency Name :  Dollar
Telephone Code :  +61
Letter Code :  AU
Letter Code :  AUS
Number :  036
Country Code TLD :  .au
-----------------------------------------------------
Order :  9
Name :  Austria
Name :  Republic of Austria
Type :  Independent State
Sub Type :  
Sovereignty :  
Capital :  Vienna
Currency Code :  EUR
Currency Name :  Euro
Telephone Code :  +43
Letter Code :  AT
Letter Code :  AUT
Number :  040
Country Code TLD :  .at

...................................

Csv csv55227.csv created

No comments:

Post a Comment