Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Need help to read data from excel sheet using filepath,sheetname,columnname, rowno
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Mayur  
View profile  
 More options Jul 7, 5:36 pm
From: Mayur <mpbongir...@gmail.com>
Date: Tue, 7 Jul 2009 00:36:33 -0700 (PDT)
Local: Tues, Jul 7 2009 5:36 pm
Subject: Need help to read data from excel sheet using filepath,sheetname,columnname,rowno
Hi all,
        I need to read data from excel sheet using
filepath,sheetname,columnname,rowno for this i had used xlrd package &
written code in python

def read_from_excel(self,filepath,sheetname,columnname,rowno):
            book=xlrd.open_workbook(filepath)
            sheetobj=book.sheet_by_name(sheetname)
            colx=sheetobj.ncols
            temp=0
            while colx:
                if sheetobj.cell_value(1,temp)
==columnname:
                    return sheetobj.cell_value(rowno,
temp)
                else:
                    colx=colx-1
                    temp=temp+1

but this function returning value none.
Please help me any one can suggest the changes in code or will send me
the new code

Thanks,
Mayur


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Георги Георгиев  
View profile  
 More options Jul 7, 6:18 pm
From: Георги Георгиев <georgi_georg...@directservices.bg>
Date: Tue, 07 Jul 2009 11:18:28 +0300
Local: Tues, Jul 7 2009 6:18 pm
Subject: Re: [pyxl] Need help to read data from excel sheet using filepath,sheetname,columnname,rowno
hey  :-)

what about

def read_from_excel(filepath, sheetname, columnname, rowno):
    sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
    for i in range(sheetobj.ncols):
        if sheetobj.cell_value(0, i) == columnname:
            return sheetobj.cell_value(rowno, i)


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MAYUR BONGIRWAR  
View profile  
 More options Jul 7, 6:57 pm
From: MAYUR BONGIRWAR <mpbongir...@gmail.com>
Date: Tue, 7 Jul 2009 14:27:51 +0530
Local: Tues, Jul 7 2009 6:57 pm
Subject: Re: [pyxl] Re: Need help to read data from excel sheet using filepath,sheetname,columnname,rowno

Thanks georgi

     But it is generating error TypeError: list indices must be integers,
not unicode

2009/7/7 Георги Георгиев <georgi_georg...@directservices.bg>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Георги Георгиев  
View profile  
 More options Jul 7, 7:08 pm
From: Георги Георгиев <georgi_georg...@directservices.bg>
Date: Tue, 07 Jul 2009 12:08:33 +0300
Local: Tues, Jul 7 2009 7:08 pm
Subject: Re: [pyxl] Re: Need help to read data from excel sheet using filepath,sheetname,columnname,rowno

hmmm strange...

the attached script works here with the attached .xls - please try it



MAYUR BONGIRWAR wrote:

Thanks georgi

     But it is generating error
TypeError: list indices must be integers, not unicode

2009/7/7 Георги Георгиев <georgi_georgiev@directservices.bg>

hey  :-)

what about


def read_from_excel(filepath, sheetname, columnname, rowno):
   sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
   for i in range(sheetobj.ncols):
       if sheetobj.cell_value(0, i) == columnname:
           return sheetobj.cell_value(rowno, i)



Mayur wrote:
> Hi all,
>         I need to read data from excel sheet using
> filepath,sheetname,columnname,rowno for this i had used xlrd package &
> written code in python
>
> def read_from_excel(self,filepath,sheetname,columnname,rowno):
>             book=xlrd.open_workbook(filepath)
>             sheetobj=book.sheet_by_name(sheetname)
>             colx=sheetobj.ncols
>             temp=0
>             while colx:
>                 if sheetobj.cell_value(1,temp)
> ==columnname:
>                     return sheetobj.cell_value(rowno,
> temp)
>                 else:
>                     colx=colx-1
>                     temp=temp+1
>
> but this function returning value none.
> Please help me any one can suggest the changes in code or will send me
> the new code
>
> Thanks,
> Mayur
>
>
> >
>





  test.py
< 1K Download

  test.xls
8K Download

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MAYUR BONGIRWAR  
View profile  
 More options Jul 7, 8:09 pm
From: MAYUR BONGIRWAR <mpbongir...@gmail.com>
Date: Tue, 7 Jul 2009 15:39:21 +0530
Local: Tues, Jul 7 2009 8:09 pm
Subject: Re: [pyxl] Re: Need help to read data from excel sheet using filepath,sheetname,columnname,rowno

Hi georgi,
             I had changed the code i have to give range in a for loop but
it is still returning me None.
I thought the numbering of column in excel sheet is from 1 instead of  0 so
i changed it but it is still returning none the code with very few
modification is

def read_from_excel(self,filepath,sheetname,columnname,rowno):

sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
            colx=sheetobj.ncols
            for i in range(1,colx):
                if sheetobj.cell_value(1, i) == columnname:
                    return sheetobj.cell_value(rowno, i)

what problem do you think I am facing.

Thanks,
Mayur

2009/7/7 Георги Георгиев <georgi_georg...@directservices.bg>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Георги Георгиев  
View profile  
 More options Jul 7, 8:35 pm
From: Георги Георгиев <georgi_georg...@directservices.bg>
Date: Tue, 07 Jul 2009 13:35:42 +0300
Local: Tues, Jul 7 2009 8:35 pm
Subject: Re: [pyxl] Re: Need help to read data from excel sheet using filepath,sheetname,columnname,rowno
could be off by one (did you tried the script with 0 instead of 1 in both places?), could be different encoding in .xls and script and probably other things :-)

can you send me sample script and .xls so i can test it?



MAYUR BONGIRWAR wrote:
Hi georgi,
             I had changed the code i have to give range in a for loop but it is still returning me None.
I thought the numbering of column in excel sheet is from 1 instead of  0 so i changed it but it is still returning none the code with very few modification is

def read_from_excel(self,filepath,sheetname,columnname,rowno):
            sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)           
            colx=sheetobj.ncols              
            for i in range(1,colx):
                if sheetobj.cell_value(1, i) == columnname:
                    return sheetobj.cell_value(rowno, i)

what problem do you think I am facing.

Thanks,
Mayur



2009/7/7 Георги Георгиев <georgi_georgiev@directservices.bg>
hmmm strange...

the attached script works here with the attached .xls - please try it




MAYUR BONGIRWAR wrote:
Thanks georgi

     But it is generating error
TypeError: list indices must be integers, not unicode

2009/7/7 Георги Георгиев <georgi_georgiev@directservices.bg>

hey  :-)

what about


def read_from_excel(filepath, sheetname, columnname, rowno):
   sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
   for i in range(sheetobj.ncols):
       if sheetobj.cell_value(0, i) == columnname:
           return sheetobj.cell_value(rowno, i)



Mayur wrote:
> Hi all,
>         I need to read data from excel sheet using
> filepath,sheetname,columnname,rowno for this i had used xlrd package &
> written code in python
>
> def read_from_excel(self,filepath,sheetname,columnname,rowno):
>             book=xlrd.open_workbook(filepath)
>             sheetobj=book.sheet_by_name(sheetname)
>             colx=sheetobj.ncols
>             temp=0
>             while colx:
>                 if sheetobj.cell_value(1,temp)
> ==columnname:
>                     return sheetobj.cell_value(rowno,
> temp)
>                 else:
>                     colx=colx-1
>                     temp=temp+1
>
> but this function returning value none.
> Please help me any one can suggest the changes in code or will send me
> the new code
>
> Thanks,
> Mayur
>
>
> >
>











    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MAYUR BONGIRWAR  
View profile  
 More options Jul 7, 8:37 pm
From: MAYUR BONGIRWAR <mpbongir...@gmail.com>
Date: Tue, 7 Jul 2009 16:07:46 +0530
Local: Tues, Jul 7 2009 8:37 pm
Subject: Re: [pyxl] Re: Need help to read data from excel sheet using filepath,sheetname,columnname,rowno

Hi Georgie,

 The problem is solved now. I am using that function in robot framework. The
robot framework sends the parameter as string so I used type casting the
problem is solved now.

Thanks for your valuable suggestions,
Mayur

2009/7/7 Георги Георгиев <georgi_georg...@directservices.bg>


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Георги Георгиев  
View profile  
 More options Jul 7, 8:41 pm
From: Георги Георгиев <georgi_georg...@directservices.bg>
Date: Tue, 07 Jul 2009 13:41:59 +0300
Local: Tues, Jul 7 2009 8:41 pm
Subject: Re: [pyxl] Re: Need help to read data from excel sheet using filepath,sheetname,columnname,rowno
i'm glad to hear that - good luck with your project :-)



MAYUR BONGIRWAR wrote:
Hi Georgie,

 The problem is solved now. I am using that function in robot framework. The robot framework sends the parameter as string so I used type casting the problem is solved now.

Thanks for your valuable suggestions,
Mayur

2009/7/7 Георги Георгиев <georgi_georgiev@directservices.bg>
could be off by one (did you tried the script with 0 instead of 1 in both places?), could be different encoding in .xls and script and probably other things :-)

can you send me sample script and .xls so i can test it?




MAYUR BONGIRWAR wrote:
Hi georgi,
             I had changed the code i have to give range in a for loop but it is still returning me None.
I thought the numbering of column in excel sheet is from 1 instead of  0 so i changed it but it is still returning none the code with very few modification is

def read_from_excel(self,filepath,sheetname,columnname,rowno):
            sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)           
            colx=sheetobj.ncols              
            for i in range(1,colx):
                if sheetobj.cell_value(1, i) == columnname:
                    return sheetobj.cell_value(rowno, i)

what problem do you think I am facing.

Thanks,
Mayur



2009/7/7 Георги Георгиев <georgi_georgiev@directservices.bg>
hmmm strange...

the attached script works here with the attached .xls - please try it




MAYUR BONGIRWAR wrote:
Thanks georgi

     But it is generating error
TypeError: list indices must be integers, not unicode

2009/7/7 Георги Георгиев <georgi_georgiev@directservices.bg>

hey  :-)

what about


def read_from_excel(filepath, sheetname, columnname, rowno):
   sheetobj=xlrd.open_workbook(filepath).sheet_by_name(sheetname)
   for i in range(sheetobj.ncols):
       if sheetobj.cell_value(0, i) == columnname:
           return sheetobj.cell_value(rowno, i)



Mayur wrote:
> Hi all,
>         I need to read data from excel sheet using
> filepath,sheetname,columnname,rowno for this i had used xlrd package &
> written code in python
>
> def read_from_excel(self,filepath,sheetname,columnname,rowno):
>             book=xlrd.open_workbook(filepath)
>             sheetobj=book.sheet_by_name(sheetname)
>             colx=sheetobj.ncols
>             temp=0
>             while colx:
>                 if sheetobj.cell_value(1,temp)
> ==columnname:
>                     return sheetobj.cell_value(rowno,
> temp)
>                 else:
>                     colx=colx-1
>                     temp=temp+1
>
> but this function returning value none.
> Please help me any one can suggest the changes in code or will send me
> the new code
>
> Thanks,
> Mayur
>
>
> >
>
















    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Machin  
View profile  
 More options Jul 7, 9:31 pm
From: John Machin <sjmac...@lexicon.net>
Date: Tue, 07 Jul 2009 21:31:57 +1000
Local: Tues, Jul 7 2009 9:31 pm
Subject: Re: [pyxl] Re: Need help to read data from excel sheet using filepath,sheetname,columnname,rowno
On 7/07/2009 8:09 PM, MAYUR BONGIRWAR wrote:

> Hi georgi,
>              I had changed the code i have to give range in a for loop
> but it is still returning me None.
> I thought the numbering of column in excel sheet is from 1 instead of  0
> so i changed it but it is still returning none the code with very few
> modification is

Hi, Mayur,

Your original problem was that you didn't have a return statement that
explicitly returned the value that you found; control flow fell off the
bottom of your function, so the default, None, was returned.

Second problem (unicode as a list index): It would help quite a lot if
you were to show the exact script that you ran, together with the full
traceback and error message.

Note that rowno should be an int; passing in a unicode string as the
rowno arg would cause that error message.

Current problem: looks like none of the column headings match your
input, so it is falling of the end of the function, and returning None.
[Please get used to typing None, not none].

xlrd is intended for Python programmers; column indexes and row indexes
start at zero.

I've added some debug print statements and some corrections into your
code below:

[untested]

DEBUG = True

def read_from_excel(self, filepath, sheetname, columnname, rowno):
     # what is `self` for? This seems like a very unlikely class method.
     IF DEBUG: print "columname: %r, rowno: %r" % (columnname, rowno)
     sheetobj = xlrd.open_workbook(filepath).sheet_by_name(sheetname)
     # colx=sheetobj.ncols
     # use meaningful names; colx means column_index
     for colx in range(sheetobj.ncols):
         value = sheetobj.cell_value(0, colx)
         if DEBUG: print "rowx=0 colx=%d value=%r" % (colx, value)
         if value == columnname:
             return sheetobj.cell_value(rowno, colx)
     raise ValueError("%s not found in first row" % columnname)

# stripped down, somewhat more Pythonic version:

def read_from_excel(filepath, sheetname, columnname, rowno):
     book = xlrd.open_workbook(filepath, on_demand=True)
     # using on_demand=True so that only 1 sheet is loaded
     sheet = book.sheet_by_name(sheetname)
     headings = sheetobj.row_values(0)
     try:
         colx = headings.index(columnname)
     except IndexError:
         raise ValueError("%s not found in first row" % columnname)
     return sheetobj.cell_value(rowno, colx)

HTH,
John


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google