[Tutor] Importing Excel sheet data

Ian Witham witham.ian at gmail.com
Thu Sep 6 06:14:37 CEST 2007


Hi Saradhi,

The first step is to export your excel data to the .csv format. This is done
in Excel from the file menu.

CSV means "comma separated values". If you have exported the data correctly
it may look something like this:

"8803","16/9/2007","299000","BEO","13B Mandeville Crescent","Grenada
Village","2.00PM","","3"
"8844","16/9/2007","599000","BEO","89 Kahu Road","Paremata","2.00PM","","4"
"8855","16/9/2007","370000","BEO","8 Salford Street","Newlands","2.00PM
","","2"

Lots of values, separated by commas, often with quotation marks containing
the values.

The code I use to import this type of data looks like this:

>>> import csv
>>> oh_reader = csv.reader(open('openhomes.csv', 'U'), dialect='excel')
>>> for row in oh_reader:
    print row


['8803', '16/9/2007', '299000', 'BEO', '13B Mandeville Crescent', 'Grenada
Village', '2.00PM', '', '3']
['8844', '16/9/2007', '599000', 'BEO', '89 Kahu Road', 'Paremata', '2.00PM',
'', '4']
['8855', '16/9/2007', '370000', 'BEO', '8 Salford Street', 'Newlands', '
2.00PM', '', '2']

There is all my data in list format.

There is also another class in csv called DictReader which presents your
data in dictionary form. To do this you must provide a list of 'keys' to use
for your dictionary. Think of these keys as being analogous to a header row
in excel. eg:

my_header_row = ['list_number', '

>>> my_header_row = ['list_number', 'date', 'price', 'price2', 'add1',
'add2', 'start', 'finish', 'bedrooms']
>>> dict_reader = csv.DictReader(open('openhomes.csv', 'U'), my_header_row,
dialect='excel')
>>> for row in dict_reader:
    print row


{'finish': '', 'add2': 'Grenada Village', 'add1': '13B Mandeville Crescent',
'price': '299000', 'list_number': '8803', 'start': '2.00PM', 'bedrooms':
'3', 'date': '16/9/2007', 'price2': 'BEO'}
{'finish': '', 'add2': 'Paremata', 'add1': '89 Kahu Road', 'price':
'599000', 'list_number': '8844', 'start': '2.00PM', 'bedrooms': '4', 'date':
'16/9/2007', 'price2': 'BEO'}
{'finish': '', 'add2': 'Newlands', 'add1': '8 Salford Street', 'price':
'370000', 'list_number': '8855', 'start': '2.00PM', 'bedrooms': '2', 'date':
'16/9/2007', 'price2': 'BEO'}

And there is my data in dictionary format. CSV also has tools for exporting
both lists and dicts to csv format again, and from there you can import the
data back to excel. I hope I have been helpful to you.

Ian.

On 9/6/07, saradhi dinavahi <saradhi.dinavahi at gmail.com> wrote:
>
> Hi Ian,
>
> I have read the Python CSV module .But I cant understand to use that
> module to Import Excel Sheet data.As I am new to the Python programming ,
> I dont know the way to  write  code using that module to meet my
> requirement.
>
> On 9/5/07, Ian Witham <witham.ian at gmail.com> wrote:
> >
> > HI Saradhi,
> >
> > I too am fairly new to Python, but I use the csv module successfully for
> > my work. Could you be a little more specific as to what your requirements
> > are and where you are finding difficulty?
> >
> > Ian.
> >
> > On 9/6/07, saradhi dinavahi <saradhi.dinavahi at gmail.com > wrote:
> > >
> > > hi Ian ,
> > >
> > > I have read the CSV module. But I felt difficult to write a code for
> > > my requirement.
> > >
> > >                   Thank You.
> > >
> > >
> > >  On 9/5/07, Ian Witham <witham.ian at gmail.com > wrote:
> > > >
> > > > Hi,
> > > >
> > > > You should look at the 'csv' module in the Python Standard Library.
> > > > If you export your excel data to csv format, you can easily import the data
> > > > in to python using the csv module.
> > > >
> > > > Ian
> > > >
> > > > On 9/6/07, saradhi dinavahi < saradhi.dinavahi at gmail.com > wrote:
> > > > >
> > > > >  hello all,
> > > > >
> > > > > I am new to the Python Programming. I want to Import Excel sheet
> > > > > data using Python. Can any one please provide me the code and explain the
> > > > > basic steps and method of executing the code.
> > > > >
> > > > >                              Thank You All
> > > > >
> > > > >
> > > > > _______________________________________________
> > > > > Tutor maillist  -   Tutor at python.org
> > > > > http://mail.python.org/mailman/listinfo/tutor
> > > > >
> > > > >
> > > >
> > >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070906/fc691c9d/attachment.htm 


More information about the Tutor mailing list