![]() |
How to read this code? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to read this code? (/thread-34661.html) |
How to read this code? - Jlyk - Aug-18-2021 Hello everybody. I'm new here and new one in Python. I have such piece of code and I don't know how to understand some parts of it. Could you please help me? from openpyxl import load_workbook, Workbook def reader(file_name, search_string1, search_string2): #this is our file read function with 2 search words book = load_workbook(file_name, read_only=True) #this is how we open it items = [ # Here I need help I don't know what is going on here. We have some list(items = []) (o.value, ) # What is this? for i in book.active.iter_rows() #Here we are going over sheet for o in i #Here we are goking over lines if o.value and isinstance(o.value, str) #Here we compare our words with cell data and search_string1.lower() in o.value.lower() and search_string2.lower() in o.value.lower() #Could anybody describe this block of code in more detail ] #Or write this piece of code for newbie language return items def writer(items): book = Workbook() sheet = book.active for row in items: sheet.append(row) book.save('result.xlsx') if __name__ == '__main__': items = reader("WEG.xlsx", '5g10', 'xpj') writer(items) RE: How to read this code? - Yoriz - Aug-18-2021 It is a List Comprehension items = [ # Here I need help I don't know what is going on here. We have some list(items = []) (o.value,) # What is this? for i in book.active.iter_rows() # Here we are going over sheet for o in i # Here we are goking over lines if o.value and isinstance(o.value, str) # Here we compare our words with cell data and search_string1.lower() in o.value.lower() and search_string2.lower() in o.value.lower() # Could anybody describe this block of code in more detail ] # Or write this piece of code for newbie languageThis is the equivalent as normal for loops items = [] for i in book.active.iter_rows(): for o in i: if ( o.value and isinstance(o.value, str) and search_string1.lower() in o.value.lower() and search_string2.lower() in o.value.lower() ): items.append((o.value,)) Edit: Included the tuple that I missed that @deanhystad reiterates below RE: How to read this code? - deanhystad - Aug-18-2021 For some reason the code builds a list of tuples. The expanded code would be: items = [] for i in book.active.iter_rows(): for o in i: if ( o.value and isinstance(o.value, str) and search_string1.lower() in o.value.lower() and search_string2.lower() in o.value.lower() ): items.append((o.value,))And to answer the other question, (o.value,) makes a tuple that contains o.value. a = 5 b = (a,) c = tuple([a]) d = (a) print(a, b, c, d) Notice that the trailing comma is required. In "b = (a,)" the parenthesis tell Python to make a Tuple. Without the comma, "d = (a)", the parenthesis are interpreted as a grouping operator. Do everything inside the parenthesis first as in x = (1 + 2) * 3 tells Python to add 1 and 2 before multiplying by 3.
RE: How to read this code? - Jlyk - Aug-19-2021 Thanks a lot guys! Appreciate your quick answers! |