0

I'm trying to read the data from excel sheet using the code

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

and the answer I'm getting is this

[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]

is there any way to skip the [u'', u'', u'', u'', values so that I can save the rest of data in another variable.

Answer: the issue was that I was directly fetching the value being unicode it got encoded with it. but if I print the index value then it comes without the u.

so, I directly used print data[0], data[1],data[2]

Hope this explains

user9144536
  • 27
  • 1
  • 11
  • Possible duplicate of [Removing u in list](https://stackoverflow.com/questions/9773121/removing-u-in-list) – Anuj TBE Dec 27 '17 at 09:53
  • ok, so in the end of code I added **print data.encode('utf-8')** and it is giving this error Traceback (most recent call last): File "mine.py", line 17, in print data.encode('utf-8') AttributeError: 'list' object has no attribute 'encode' – user9144536 Dec 27 '17 at 10:05
  • Iterate through each element. You can **encode** and **decode** a string, but can a list ? ;) – IMCoins Dec 27 '17 at 10:18
  • but won't iterating through each element remove the u values from the normal string. And I think if it works on string it should work on list @IMCoins – user9144536 Dec 27 '17 at 10:20
  • @user9144536 What's your expected output? – Stop harming Monica Dec 27 '17 at 10:56

1 Answers1

0

You can use filtered_data = list(filter(None, data)) or you can loop over list to check if it is empty with filtered_data = [a for a in data if a != ""] then you can print filtered_data to see result.

Deniz Kaplan
  • 1,549
  • 1
  • 13
  • 18
  • got this error, File "mine.py", line 18 print list ^ SyntaxError: invalid syntax can you tell me how to write it or where exactly I'm going wrong _data= ws.row_values(rows) list(filter(None, data) print list_ – user9144536 Dec 27 '17 at 10:16
  • `for rows in range(0,14): data= ws.row_values(rows) print list(filter(None, data))` this works for me with your inputs, you didn't assign list(filter(None, data)) as I see above. – Deniz Kaplan Dec 27 '17 at 10:24
  • I updated my answer and set filtered data to a variable as asked above. – Deniz Kaplan Dec 27 '17 at 10:36
  • it somewhat worked, got this as answer [u'RS2 WW10 BKC', u'RS2 WW23.2 BKC'] [u'Slno', u'Title', u'Platform Target(W)', u'SoC Target (mW)', u'Platform Power( W)', and so on, could you tell me @DenizKaplan how to the u and the bracket and the quotes part – user9144536 Dec 27 '17 at 12:01
  • The `u` part is for unicode string, but you can replace all brackets with `string.replace` method. `u'Platform Power( W)'.replace("(","").replace(")","")` will return `u"Platform Power W"` the quotes are also represents string type. I hope this will help you, if I understand your question correctly. – Deniz Kaplan Dec 27 '17 at 12:23
  • should I put in an if condition for that because there is going a lot, I mean huge amounts of data, and doing it one at a time will be difficult and time consuming. Also I want to get rid of the "u" character that is coming infront of every character. You earlier loop helped but no completely. Also in the loop can you tell me what is x? – user9144536 Dec 28 '17 at 07:04
  • I've changed 'x' with 'data'. You cannot exactly get rid of the "u" character (but you won't see if you print any element in array), because it's python's unicode string literal (not exactly a character in your data). You can use replace as `filtered_data = [a.replace("(","").replace(")","") for a in data if a != ""]` without if statement. If you have big data and want something else for time consumption, I can recommend https://pandas.pydata.org/ – Deniz Kaplan Dec 28 '17 at 07:35
  • Thanks @DenizKaplan, you're last line gave me an idea, I'll edit and post the solution – user9144536 Dec 28 '17 at 09:01