使用for和if遍历openpyxl中的行

时间:2017-07-22 17:51:20

标签: python for-loop openpyxl

我一直在寻找,但找不到我问题的明显答案。

我正在使用openpyxl通过以下调用从read_only = True的工作表中读取数据:

for row in ws.rows:
    for col in row:
          npAvailability[row_idx, col_idx] = col.value
          col_idx += 1
    row_idx += 1

但是,我想跳过工作表中的第一行,并想知道是否可以在第一个for调用中执行此操作,可能看起来像这样:

for row in ws.rows if row_idx >= 1:

显然这不起作用。唯一的另一种方法是在行调用之后执行if,例如:

for row in ws.row:
    if row_idx >= 1:
         for col in row:
             etc...

这看起来很麻烦。指导将不胜感激。

P上。

3 个答案:

答案 0 :(得分:1)

切掉第一行怎么样?

for row in ws.rows[1:]:
    ...

注意:这是未经测试的,但从source code开始。

答案 1 :(得分:0)

您可以直接使用迭代器跳过第一行。

    Bus 002 Device 019: ID 0b05:182c ASUSTek Computer, Inc. 
    Bus 002 Device 018: ID 0b05:182d ASUSTek Computer, Inc. 
    Bus 002 Device 017: ID 0bda:0401 Realtek Semiconductor Corp. 
    Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
    Bus 001 Device 005: ID 8087:0a2b Intel Corp. 
    Bus 001 Device 004: ID 1c7a:0603 LighTuning Technology Inc. 
    Bus 001 Device 009: ID 062a:4101 Creative Labs Wireless Keyboard/Mouse
    Bus 001 Device 013: ID 0bda:5401 Realtek Semiconductor Corp. RTL 8153 USB 3.0 hub with gigabit ethernet

The output of "dmesg -n 50" is :

[ 1535.643456] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 3: -110 (exp. 2).
[ 1535.943470] uvcvideo: Failed to query (GET_LEN) UVC control 3 on unit 3: -110 (exp. 2).
[ 1536.243469] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 3: -110 (exp. 2).
[ 1536.972591] usb 2-3.1: Not enough bandwidth for new device state.
[ 1536.972595] usb 2-3.1: Not enough bandwidth for altsetting 10
[ 1828.081983] usbcore: deregistering interface driver uvcvideo
[ 1828.434872] uvcvideo: Found UVC 1.00 device ASUS Xtion2 RGB (0b05:182d)
[ 1828.434876] uvcvideo: Forcing device quirks to 0x80 by module parameter for testing purpose.
[ 1828.434878] uvcvideo: Please report required quirks to the linux-uvc-devel mailing list.
[ 1828.635409] input: ASUS Xtion2 RGB as /devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3.1/2-3.1:1.0/input/input83
[ 1828.642270] uvcvideo: Found UVC 1.00 device ASUS Xtion2 Depth (0b05:182c)
[ 1828.642277] uvcvideo: Forcing device quirks to 0x80 by module parameter for testing purpose.
[ 1828.642281] uvcvideo: Please report required quirks to the linux-uvc-devel mailing list.
[ 1828.646964] input: ASUS Xtion2 Depth as /devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3.2/2-3.2:1.0/input/input84
[ 1828.649096] usbcore: registered new interface driver uvcvideo
[ 1828.649098] USB Video Class driver (1.1.1)
[ 2010.073664] uvcvideo: Found UVC 1.00 device ASUS Xtion2 RGB (0b05:182d)
[ 2010.073669] uvcvideo: Forcing device quirks to 0x80 by module parameter for testing purpose.
[ 2010.073671] uvcvideo: Please report required quirks to the linux-uvc-devel mailing list.
[ 2010.273866] input: ASUS Xtion2 RGB as /devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3.1/2-3.1:1.0/input/input85
[ 2010.327318] uvcvideo: Found UVC 1.00 device ASUS Xtion2 Depth (0b05:182c)
[ 2010.327320] uvcvideo: Forcing device quirks to 0x80 by module parameter for testing purpose.
[ 2010.327321] uvcvideo: Please report required quirks to the linux-uvc-devel mailing list.
[ 2010.331978] input: ASUS Xtion2 Depth as /devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3.2/2-3.2:1.0/input/input86
[ 2010.688232] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 3: -110 (exp. 2).
[ 2010.988243] uvcvideo: Failed to query (GET_LEN) UVC control 3 on unit 3: -110 (exp. 2).
[ 2011.288244] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 3: -110 (exp. 2).
[ 2011.588260] uvcvideo: Failed to query (GET_LEN) UVC control 3 on unit 3: -110 (exp. 2).
[ 2011.888286] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 3: -110 (exp. 2).
[ 2012.192254] uvcvideo: Failed to query (GET_LEN) UVC control 3 on unit 3: -110 (exp. 2).
[ 2012.492299] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 3: -110 (exp. 2).
[ 2013.217647] usb 2-3.1: Not enough bandwidth for new device state.
[ 2013.217656] usb 2-3.1: Not enough bandwidth for altsetting 10
[ 3827.127184] uvcvideo: Found UVC 1.00 device ASUS Xtion2 RGB (0b05:182d)
[ 3827.127188] uvcvideo: Forcing device quirks to 0x80 by module parameter for testing purpose.
[ 3827.127189] uvcvideo: Please report required quirks to the linux-uvc-devel mailing list.
[ 3827.327948] input: ASUS Xtion2 RGB as /devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3.1/2-3.1:1.0/input/input87
[ 3827.404875] uvcvideo: Found UVC 1.00 device ASUS Xtion2 Depth (0b05:182c)
[ 3827.404879] uvcvideo: Forcing device quirks to 0x80 by module parameter for testing purpose.
[ 3827.404881] uvcvideo: Please report required quirks to the linux-uvc-devel mailing list.
[ 3827.409851] input: ASUS Xtion2 Depth as /devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3.2/2-3.2:1.0/input/input88
[ 3827.777065] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 3: -110 (exp. 2).
[ 3828.077111] uvcvideo: Failed to query (GET_LEN) UVC control 3 on unit 3: -110 (exp. 2).
[ 3828.377078] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 3: -110 (exp. 2).
[ 3828.677125] uvcvideo: Failed to query (GET_LEN) UVC control 3 on unit 3: -110 (exp. 2).
[ 3828.977091] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 3: -110 (exp. 2).
[ 3829.277098] uvcvideo: Failed to query (GET_LEN) UVC control 3 on unit 3: -110 (exp. 2).
[ 3829.577147] uvcvideo: Failed to query (SET_CUR) UVC control 2 on unit 3: -110 (exp. 2).
[ 3830.307050] usb 2-3.1: Not enough bandwidth for new device state.
[ 3830.307053] usb 2-3.1: Not enough bandwidth for altsetting 10

答案 2 :(得分:0)

在我的情况下,需要使用(CASE WHEN date2 > par_final THEN par_final ELSE date2 END - CASE WHEN date1 > par_initial THEN par_initial ELSE date1 END) / 365 AS exposure 方法,如下例所示:

iter_rows

我正在使用openpyxl版本3.0.0