【问题标题】:Nested for loop being ignored嵌套的 for 循环被忽略
【发布时间】:2021-04-13 07:07:27
【问题描述】:

我有一个嵌套的 for 循环,但是它被忽略了,我不知道为什么。

import pandas as pd
import shutil
import os
import glob

source_dir = r'C:\Users\Ahmed_Abdelmuniem\Desktop\GLift'
target_dir = r'C:\Users\Ahmed_Abdelmuniem\Desktop\TAfternoon'

file_names = glob.glob(os.path.join(source_dir, '*.html'))

for file_name in file_names:

    table = pd.read_html(file_name)
    print ('tables found:', len(table))
    for table in file_name:
        filtered_table = [df for df in table if len(df) > 2]
        for df in filtered_table:
            comp = df.iat[1, 0]
            if comp == 'SLS':
                name = df.iat[0,0]
                print (name)

输出如下:

:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\python.exe "C:/Users/Ahmed_Abdelmuniem/PycharmProjects/File mover V2.0/main.py"
tables found: 73
tables found: 73
tables found: 71
tables found: 72
tables found: 123
tables found: 124
tables found: 123
tables found: 124
tables found: 123
tables found: 72
tables found: 72
tables found: 123
tables found: 121
tables found: 122
tables found: 119
tables found: 125
tables found: 124
tables found: 121
tables found: 121
tables found: 120
tables found: 122
tables found: 121

Process finished with exit code 0

如您所见,它只执行第一个 for 循环,而不是第二个,因为它不打印 name 变量。

编辑:为了澄清,我已经证明代码在不嵌套时可以工作。

这是单独工作以提取数据的代码,但是在嵌套时它不起作用。

import pandas as pd
import xlwt
from xlwt import Workbook
from openpyxl import load_workbook
fn = r'C:\Users\Ahmed_Abdelmuniem\PycharmProjects\Pandas Parser\Survey.xlsx'



file = r'C:\Users\Ahmed_Abdelmuniem\Desktop\XXX.html'
table = pd.read_html(file)


i=3

filtered_table = [df for df in table if len(df) > 2]

for df in filtered_table:
    comp = df.iat[1,0]

    if comp == 'SLS' :
        name = df.iat[0,0]
        print (name)

这是另一个代码的输出,表明那里确实有数据。

C:\Users\Ahmed_Abdelmuniem\AppData\Local\Programs\Python\Python39\python.exe "C:/Users/Ahmed_Abdelmuniem/PycharmProjects/Pandas Parser/main.py"
INW
SPL

Process finished with exit code 0

【问题讨论】:

  • 在内循环中的打印语句之前需要满足几个条件。例如,filtered_table 必须至少有一项和comp == 'SLS'。你怎么知道内部循环没有运行而不是那些条件没有被满足?
  • 检查filtered_table列表是否有数据或者是否为空。之后检查是否有任何数据满足您的第二个 if 条件 (comp == 'SLS')
  • 我已经检查过它是否在另一个代码中实现,我现在只是将单个代码组合成一个更大的代码。

标签: python python-3.x pandas


【解决方案1】:

在您的以下工作代码中

file = r'C:\Users\Ahmed_Abdelmuniem\Desktop\XXX.html'
table = pd.read_html(file)

filtered_table = [df for df in table if len(df) > 2]

for df in filtered_table:
    comp = df.iat[1,0]

    if comp == 'SLS' :
        name = df.iat[0,0]
        print (name)

你在做,

  1. pd.read_html(file)读表
  2. 使用[df for df in table if len(df) > 2] 过滤表

然而,在你的嵌套循环中,你正在做

  1. pd.read_html(file_name)读表
  2. 未知的输入for table in file_name
  3. 使用[df for df in table if len(df) > 2] 过滤表
for file_name in file_names:
    table = pd.read_html(file_name)
    print ('tables found:', len(table))
    for table in file_name:
        filtered_table = [df for df in table if len(df) > 2] 
        #                               ^
        #                               |
        #                              table here is file_name
        for df in filtered_table:
            comp = df.iat[1, 0]
            if comp == 'SLS':
                name = df.iat[0,0]
                print (name)

您可能需要删除 for table in file_name

for file_name in file_names:

    table = pd.read_html(file_name)
    print ('tables found:', len(table))

    filtered_table = [df for df in table if len(df) > 2]
    for df in filtered_table:
        comp = df.iat[1, 0]
        if comp == 'SLS':
            name = df.iat[0,0]
            print (name)

【讨论】:

  • 好吧哇,它成功了,但是为什么'for table in file_name'是多余的?
  • @Ahmed 如果不是多余的,你想做什么?
  • 我的意思是,我不明白它是如何冗余的逻辑?
  • @Ahmed 通过使用for table in file_name,您正在循环file_name 字符串中的每个字符。
  • 非常感谢您的解决方案和澄清。您似乎是这个社区中非常有价值的成员,您提供解决方案和课程。保持祝福。
猜你喜欢
  • 2019-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多