Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
def function
#6
(Jul-22-2020, 07:02 PM)jefsummers Wrote: But that is also the name of your function. In some languages you define your return value by setting it to the function name. That is not true with Python.
Please recognize that by having the return inside an if statement it returns the value only if the first obj.value is equal to the_string. If not, it simply drops to the end of the function and returns nothing.
You could print the value rather than return it (generally bad idea), or build a list of values that match and return that. The latter would be
t_file = xl.load_workbook(T)
sheet = t_file.worksheets[0]
 
big_string=pd.read_excel(input_file, usecols="A",nrows=1,header=None,squeeze=True).str.slice(start=28,stop=-2).to_string(index=False).strip()
    
def ta(the_string, the_sheet):
    out_list = []
    for cellobj in the_sheet['B2:B10000']:
        for obj in cellobj:
            if (obj.value) == the_string:
                outlist.append(obj.offset(column=2, row=6).value)
    return out_list  
#TA = obj.offset I don't know what you are trying to do here
print(ta(big_string, sheet))

When I use this it prints out [None]

This is what I had at first. This finds my string and prints it out A1180. Now that I found the string I need to get the string that is 9 cells below the first string same column, and thats where I though offset could be used. Or am I going about this completely wrong?

  
TrainFile = xl.load_workbook(Train)
TrainFileSheet = TrainFile.worksheets[0]
String=pd.read_excel(input_file, usecols="A",nrows=1,header=None,squeeze=True).str.slice(start=28,stop=-2).to_string(index=False).strip()
 

for cellObj in TrainFileSheet['B2:B10000']:
     for obj in cellObj:
         if (obj.value) == String:
         TA = obj.value
print(TA)
A1180
   
Reply


Messages In This Thread
def function - by Kristenl2784 - Jul-22-2020, 06:06 PM
RE: def function - by Larz60+ - Jul-22-2020, 06:21 PM
RE: def function - by jefsummers - Jul-22-2020, 06:31 PM
RE: def function - by Kristenl2784 - Jul-22-2020, 06:36 PM
RE: def function - by jefsummers - Jul-22-2020, 07:02 PM
RE: def function - by Kristenl2784 - Jul-22-2020, 08:12 PM
RE: def function - by jefsummers - Jul-23-2020, 01:06 AM
RE: def function - by Kristenl2784 - Jul-23-2020, 11:40 AM
RE: def function - by jefsummers - Jul-23-2020, 05:26 PM
RE: def function - by Kristenl2784 - Jul-23-2020, 05:55 PM
RE: def function - by Kristenl2784 - Jul-23-2020, 06:58 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020