在python中跳过空值

问题描述 投票:0回答:1

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

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',值,以便我可以将其余数据保存在另一个变量中。

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

所以,我直接使用打印数据[0],数据[1],数据[2]

希望这能解释

python excel xlrd
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.