0

Error

I downloaded Python and this is the result. input: from xlrd import open_workbook wb = open_workbook('Book2.xls') values = [] for s in wb.sheets(): #print 'Sheet:',s.name for row in range(1, s.nrows): col_names = s.row(0) col_value = [] for name, col in zip(col_names, range(s.ncols)): value = (s.cell(row,col).value) try : value = str(int(value)) except : pass col_value.append((name.value, value)) values.append(col_value) print (values) Output: Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from xlrd import open_workbook Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named xlrd >>> >>> wb = open_workbook('Book2.xls') Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'open_workbook' is not defined >>> values = [] >>> for s in wb.sheets(): ... #print 'Sheet:',s.name ... for row in range(1, s.nrows): ... col_names = s.row(0) ... col_value = [] ... for name, col in zip(col_names, range(s.ncols)): ... value = (s.cell(row,col).value) ... try : value = str(int(value)) ... except : pass ... col_value.append((name.value, value)) ... values.append(col_value) ... print (values) File "<stdin>", line 12 print (values) ^ SyntaxError: invalid syntax >>>

28th Jan 2018, 7:44 PM
Steven
Steven - avatar
15 odpowiedzi
+ 4
Could you save this to a code here on Sololearn? It's hard to check the code in the comments, because they are not displayed in a monispace type font. Perhaps there is something wrong with the punctuation, but I can't spot it like that.
29th Jan 2018, 7:58 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 4
Hmm... I have to check it on the desktop installation, not mobile. As I take it, it should work alright, provided the module indeed has the methods you use.
31st Jan 2018, 7:23 AM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 3
First of all, this is Python 2.7 which is outdated. You should switch to Python 3.6 already :) Second of all, you don't seem to have the xlrd module installed. You should "pip install" it from your console. Finally, let me know how is xlrd doing. I've been using openpyxl instead, but maybe there's a better alternative :)
28th Jan 2018, 8:19 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 3
TLDR: Python 2.x is legacy, Python 3.x is the present and future of the language A bit more info: https://wiki.python.org/moin/Python2orPython3
28th Jan 2018, 8:33 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 3
Oookay... Now, run "pip install xlrd" That will actually download the needed module and make it ready for you to use (and import).
28th Jan 2018, 8:56 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
+ 3
Come on, now... Read what the interpreter is telling you. Do you happen to have the actual XLS file at hand and in the same directory/folder that you run the script in? FileNotFoundError: [Errno 2] No such file or directory: 'Book2.xls'
28th Jan 2018, 9:04 PM
Kuba SiekierzyƄski
Kuba SiekierzyƄski - avatar
0
@Kuba SiekierzyƄski I just downloaded Python a couple of hours ago from www.python.org, how is it out of date?!
28th Jan 2018, 8:23 PM
Steven
Steven - avatar
0
I have pip installed Microsoft Windows [Version 10.0.16299.192] (c) 2017 Microsoft Corporation. All rights reserved. C:\Users\Steven Ellet>pip Usage: pip <command> [options] Commands: install Install packages. download Download packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. list List installed packages. show Show information about installed packages. check Verify installed packages have compatible dependencies. search Search PyPI for packages. wheel Build wheels from your requirements. hash Compute hashes of package archives. completion A helper command used for command completion. help Show help for commands. General Options: -h, --help Show help. --isolated Run pip in an isolated mode, ignoring environment variables and user configuration. -v, --verbose Give more output. Option is additive, and can be used up to 3 times. -V, --version Show version and exit. -q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels). --log <path> Path to a verbose appending log. --proxy <proxy> Specify a proxy in the form [user:passwd@]proxy.server:port. --retries <retries> Maximum number of retries each connection should attempt (default 5 times). --timeout <sec> Set the socket timeout (default 15 seconds). --exists-action <action> Default action when a pa
28th Jan 2018, 8:52 PM
Steven
Steven - avatar
0
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from xlrd import open_workbook Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'xlrd' >>> >>> wb = open_workbook('Book2.xls') Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'open_workbook' is not defined >>> values = [] >>> for s in wb.sheets(): ... #print 'Sheet:',s.name ... for row in range(1, s.nrows): ... col_names = s.row(0) ... col_value = [] ... for name, col in zip(col_names, range(s.ncols)): ... value = (s.cell(row,col).value) ... try : value = str(int(value)) ... except : pass ... col_value.append((name.value, value)) ... values.append(col_value) ... print (values) File "<stdin>", line 12 print (values) ^ SyntaxError: invalid syntax >>> >>>
28th Jan 2018, 8:54 PM
Steven
Steven - avatar
0
done Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from xlrd import open_workbook >>> >>> wb = open_workbook('Book2.xls') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Program Files\Python\Python36\lib\site-packages\xlrd\__init__.py", line 116, in open_workbook with open(filename, "rb") as f: FileNotFoundError: [Errno 2] No such file or directory: 'Book2.xls' >>> values = [] >>> for s in wb.sheets(): ... #print 'Sheet:',s.name ... for row in range(1, s.nrows): ... col_names = s.row(0) ... col_value = [] ... for name, col in zip(col_names, range(s.ncols)): ... value = (s.cell(row,col).value) ... try : value = str(int(value)) ... except : pass ... col_value.append((name.value, value)) ... values.append(col_value) ... print (values) File "<stdin>", line 12 print (values) ^ SyntaxError: invalid syntax >>>
28th Jan 2018, 9:01 PM
Steven
Steven - avatar
0
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:54:40) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> from xlrd import open_workbook >>> >>> wb = open_workbook('Book2.xls') >>> values = [] >>> for s in wb.sheets(): ... #print 'Sheet:',s.name ... for row in range(1, s.nrows): ... col_names = s.row(0) ... col_value = [] ... for name, col in zip(col_names, range(s.ncols)): ... value = (s.cell(row,col).value) ... try : value = str(int(value)) ... except : pass ... col_value.append((name.value, value)) ... values.append(col_value) ... print (values) File "<stdin>", line 12 print (values) ^ SyntaxError: invalid syntax >>> >>>
28th Jan 2018, 9:46 PM
Steven
Steven - avatar
29th Jan 2018, 3:45 PM
Steven
Steven - avatar
0
@Kuba SiekierzyƄski you still there?
31st Jan 2018, 1:43 AM
Steven
Steven - avatar
0
the problem was the "print (values)" on line 16, I still can't get it to work properly though.
31st Jan 2018, 5:29 AM
Steven
Steven - avatar
0
I'm trying something a little different import openpyxl wb = openpyxl.load_workbook('example.xlsx') sheet = wb.get_sheet_by_name('Sheet2') c = sheet['B2'] c.value this works well for retrieving one cell at a time and by changing c = sheet['B2'] I can change what I retrieve, but I want to retrieve many cells quickly.
2nd Feb 2018, 5:26 PM
Steven
Steven - avatar