Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to read this code?
#1
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)
Reply
#2
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 language
This 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
Reply
#3
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)
Output:
5 (5,) (5,) 5
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.
Reply
#4
Thanks a lot guys! Appreciate your quick answers!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,353 Feb-13-2022, 07:13 PM
Last Post: menator01
  No output for the code to read emails avani9659 6 4,150 Aug-14-2018, 08:30 AM
Last Post: avani9659
  How to Make Python code execution pause and resume to create .csv and read values. Kashi 2 3,716 Jun-14-2018, 04:16 PM
Last Post: DeaD_EyE
  Cant find S3 Read or Write access from Python code tpc 0 2,343 Apr-19-2018, 04:00 AM
Last Post: tpc

Forum Jump:

User Panel Messages

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