跳过 python 中的空值

Skipping the null values in python

我正在尝试从 excel sheet 中读取数据 使用代码

import xlrd

input= raw_input("put in the path name for file")
book=xlrd.open_workbook(input)
print book.nsheets
print book.sheet_names()

ws=book.sheet_by_index(0)
for rows in range(0,14):
    data= ws.row_values(rows)
    print data

我得到的答案是

[u'', u'', u'', u'', u'RS2 WW10 BKC', u'', u'RS2 WW23.2 BKC', u'']
[u'Slno', u'Title', u'Platform Target(W)', u'SoC Target (mW)', u'Platform Power(
W)', u'SoC  Power (W)', u'Platform Power(W)', u'SoC  Power (W)']
[u'', u'', u'', u'', u'', u'', u'', u'']
[1.0, u'Display Idle', 4766.0, 80.0, 5013.0, 145.0, 2714.0, 2422.0]
[2.0, u'VPB 1080p_30fps', 4999.0, 615.0, 4726.0, 1219.0, 2800.0, 2470.0]
[3.0, u'VPB _4k2k-H265', 5888.0, 1231.0, 5244.0, 1619.0, 0.0, 0.0]
[4.0, u'Connected standby', 181.0, 17.0, 266.0, 124.0, 176.0, 74.0]
[5.0, u'S3', 193.0, 52.0, 132.0, 94.0, 3719.0, 3245.0]
[6.0, u'Angry Bird', 6804.0, 2118.0, 7730.0, 3860.0, 2668.0, 2387.0]
[7.0, u'Browsing bench', 6209.0, 474.0, 5851.0, 1241.0, 3207.0, 2813.0]
[8.0, u'Dash Streaming', 5411.0, 663.0, 5576.0, 1842.0, 0.0, 0.0]
[9.0, u'MM14', 5651.0, 973.0, 7504.0, 3024.0, 0.0, 0.0]
[10.0, u'Miracast-VPB_Extend', 5072.0, 4007.0, 4196.0, 3081.0, 0.0, 0.0]
[11.0, u'Miracast-Idle', 5389.0, 756.0, 0.0, 0.0, 0.0, 0.0]

有什么办法可以跳过 [u'', u'', u'', u'', values 以便我可以将其余数据保存在另一个变量中。

回答:问题是我直接获取的值是用它编码的 unicode。但是如果我打印索引值那么它就没有 u.

所以,我直接用print data[0],data[1],data[2]

希望这能解释

您可以使用 filtered_data = list(filter(None, data)) 或者您可以遍历列表以检查它是否为空 filtered_data = [a for a in data if a != ""] 然后你可以打印 filtered_data 来查看结果。