load_workbook giving value error for string literal - openpyxl

So I am writing a simple test code in python, but I cant seem to load my workbook to read and write into. The workbook I am trying to open has a lot of data and is linked to an Access Database. I am also using Python 2.7.
Here is the code:
from openpyxl import Workbook
from openpyxl import load_workbook
print("here1")
wb = load_workbook("Z:\\PythonFiles\\testexcel2.xlsx")
print("here2")
ws1 = wb.get_sheet_by_name("Output Model")
import datetime
print("here3")
ws1.cell(row=3, column=9).value="Somevalue2"
wb.save("Z:\\PythonFiles\\testexcel2.xlsx")
Now here is the error:
here1
Traceback (most recent call last):
File "Z:\PythonFiles\Test.py", line 4, in <module>
wb = load_workbook("Z:\\PythonFiles\\testexcel2.xlsx")
File "C:\Python27\openpyxl\reader\excel.py", line 233, in load_workbook
parser.parse()
File "C:\Python27\openpyxl\reader\worksheet.py", line 104, in parse
dispatcher[tag_name](element)
File "C:\Python27\openpyxl\reader\worksheet.py", line 235, in parse_row_dimensions
self.parse_cell(cell)
File "C:\Python27\openpyxl\reader\worksheet.py", line 191, in parse_cell
value = _cast_number(value)
File "C:\Python27\openpyxl\reader\worksheet.py", line 55, in _cast_number
return int(value)
ValueError: invalid literal for int() with base 10: '1E-3'

This was a bug in the code which has been fixed. https://bitbucket.org/openpyxl/openpyxl/commits/8cc875b206b08895a1a0ae9ac33cb997a941eddf

Related

backtrader #IndexError: list index out of range problem

The data is coming in as unix timestamp in milliseconds.
So i am using #dtformat=lambda x: datetime.datetime.utcfromtimestamp(int(x) / 1000).
import backtrader as bt
import datetime
cerebro = bt.Cerebro()
data = bt.feeds.GenericCSVData(dataname="ftx1h.csv", dtformat=lambda x: datetime.datetime.utcfromtimestamp(int(x) / 1000))
cerebro.adddata(data)
cerebro.run()
cerebro.plot()
But now there is an #IndexError: list index out of range problem.
Traceback (most recent call last):
File "/Users/talha/VScode/Backtest/Strategy5.py", line 7, in
cerebro.run()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/cerebro.py", line 1127, in run
runstrat = self.runstrategies(iterstrat)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/cerebro.py", line 1212, in runstrategies
data.preload()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/feed.py", line 688, in preload
while self.load():
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/feed.py", line 479, in load
_loadret = self._load()
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/feed.py", line 710, in _load
return self._loadline(linetokens)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/backtrader/feeds/csvgeneric.py", line 148, in _loadline
csvfield = linetokens[csvidx]
IndexError: list index out of range
The Data
1660435200000,24454.0,24528.0,24355.0,24527.0,67147303.3027
1660438800000,24527.0,24682.0,24422.0,24598.0,116587503.2913
1660442400000,24598.0,24632.0,24514.0,24608.0,34892443.4722
1660446000000,24608.0,24635.0,24553.0,24584.0,22989205.8046
1660449600000,24584.0,24652.0,24506.0,24581.0,44973942.2299
1660453200000,24581.0,24636.0,24540.0,24603.0,47360860.8874
1660456800000,24613.0,24695.0,24603.0,24689.0,32459071.9839
1660460400000,24689.0,25065.0,24682.0,24900.0,301312472.1925
1660464000000,24900.0,24902.0,24608.0,24673.0,170952143.2637
1660467600000,24672.0,24790.0,24643.0,24758.0,70969336.6324
1660471200000,24758.0,24759.0,24618.0,24627.0,64221863.5382
1660474800000,24627.0,24682.0,24525.0,24569.0,76070172.2453
1660478400000,24569.0,24593.0,24475.0,24535.0,59713290.9481
1660482000000,24535.0,24538.0,24406.0,24481.0,89923365.2962
1660485600000,24481.0,24534.0,24466.0,24518.0,49835330.6131
1660489200000,24518.0,24549.0,24468.0,24528.0,46556537.1289
1660492800000,24528.0,24560.0,24313.0,24315.0,108465182.8929
1660496400000,24315.0,24332.0,24178.0,24232.0,176757581.0963
1660500000000,24232.0,24300.0,24155.0,24282.0,68322221.4964
1660503600000,24281.0,24327.0,24244.0,24289.0,34982570.2998
1660507200000,24289.0,24352.0,24255.0,24315.0,40799224.029
1660510800000,24315.0,24448.0,24278.0,24349.0,94709768.3767
1660514400000,24349.0,24372.0,24182.0,24265.0,75704499.2116
1660518000000,24265.0,24358.0,24240.0,24310.0,46011851.1185
1660521600000,24310.0,24418.0,24171.0,24406.0,90810435.293
1660525200000,24406.0,24665.0,24340.0,24655.0,233812433.3012
1660528800000,24654.0,24950.0,24653.0,24937.0,223521446.7061
1660532400000,24937.0,25232.0,24813.0,24883.0,373106086.273
1660536000000,24883.0,24900.0,24696.0,24863.0,172798798.982
1660539600000,24863.0,24998.0,24762.0,24835.0,110243603.1486
1660543200000,24835.0,24841.0,24088.0,24129.0,338761936.3576
1660546800000,24130.0,24218.0,23950.0,24026.0,287354302.5387
1660550400000,24025.0,24089.0,23873.0,24044.0,151992609.2842
1660554000000,24044.0,24210.0,23992.0,24176.0,132115004.5245
1660557600000,24180.0,24300.0,24133.0,24275.0,88762538.3025
1660561200000,24275.0,24316.0,24009.0,24057.0,116716391.6228
1660564800000,24057.0,24197.0,23891.0,23966.0,135401782.9878
Your data is all one really long line, at least when I pasted it into geany. Are there any line endings? Could just be a problem with my browser. This might be your problem.

How to get the code of a module without importing the module?

I want to create a file that copies all the code from each module and pastes it into a new file. Here's what I have so far:
from stdlib_list import stdlib_list
import inspect as getcode
libs = stdlib_list("3.8")
for i in range(len(libs)):
system(f"import {libs[i]}")
f = open(f"{libs[i]}_module.py", "w+")
code = getcode.getsource(libs[i])
f.write(code)
f.close()
But I get this error:
sh: import: command not found
Traceback (most recent call last):
File "/Users/pengguest/get_mouldule_code.py", line 8, in <module>
code = getcode.getsource(libs[i])
File "/Users/pengli/opt/anaconda3/lib/python3.8/inspect.py", line 985, in getsource
lines, lnum = getsourcelines(object)
File "/Users/pengli/opt/anaconda3/lib/python3.8/inspect.py", line 967, in getsourcelines
lines, lnum = findsource(object)
File "/Users/pengli/opt/anaconda3/lib/python3.8/inspect.py", line 780, in findsource
file = getsourcefile(object)
File "/Users/pengli/opt/anaconda3/lib/python3.8/inspect.py", line 696, in getsourcefile
filename = getfile(object)
File "/Users/pengli/opt/anaconda3/lib/python3.8/inspect.py", line 676, in getfile
raise TypeError('module, class, method, function, traceback, frame, or '
TypeError: module, class, method, function, traceback, frame, or code object was expected, got str
[Finished in 0.6s with exit code 1]
[shell_cmd: python -u "/Users/pengguest/get_mouldule_code.py"]
[dir: /Users/pengguest]
[path: /usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin]
Can anyone help? Thanks.

TypeError: expected string or bytes-like object when using NLTK word_tokenize

I am trying to import a CSV file and then using NLTK to analyse the text. The CSV file contain several columns but now I only want to analyse one column in this file so far.
The sample of csv file is:
sample data from csv file
The code for read CSV file and using the word_tokenize are below:
import pandas as pd
import nltk
#nltk.download('all')
data=pd.read_csv("Output-analysis.csv")
print (data.SAT_COMMENTS)
from nltk.tokenize import word_tokenize
tokenize_word=word_tokenize(data.SAT_COMMENTS)
print(tokenize_word)
It seems I can read and print the SAT_Comment column no problem, but when I try to use word_tokenize, it is pointing there are some lines in csv file and then TypeError: expected string or bytes-like object error.
error details:
Traceback (most recent call last):
File "C:\Users\Rachel\Desktop\SAT analysis\Attempts.py", line 22, in <module>
tokenize_word=word_tokenize(data.SAT_COMMENTS)
File "C:\Users\Rachel\AppData\Local\Programs\Python\Python38\lib\site-packages\nltk\tokenize\__init__.py", line 129, in word_tokenize
sentences = [text] if preserve_line else sent_tokenize(text, language)
File "C:\Users\Rachel\AppData\Local\Programs\Python\Python38\lib\site-packages\nltk\tokenize\__init__.py", line 107, in sent_tokenize
return tokenizer.tokenize(text)
File "C:\Users\Rachel\AppData\Local\Programs\Python\Python38\lib\site-packages\nltk\tokenize\punkt.py", line 1272, in tokenize
return list(self.sentences_from_text(text, realign_boundaries))
File "C:\Users\Rachel\AppData\Local\Programs\Python\Python38\lib\site-packages\nltk\tokenize\punkt.py", line 1326, in sentences_from_text
return [text[s:e] for s, e in self.span_tokenize(text, realign_boundaries)]
File "C:\Users\Rachel\AppData\Local\Programs\Python\Python38\lib\site-packages\nltk\tokenize\punkt.py", line 1326, in <listcomp>
return [text[s:e] for s, e in self.span_tokenize(text, realign_boundaries)]
File "C:\Users\Rachel\AppData\Local\Programs\Python\Python38\lib\site-packages\nltk\tokenize\punkt.py", line 1316, in span_tokenize
for sl in slices:
File "C:\Users\Rachel\AppData\Local\Programs\Python\Python38\lib\site-packages\nltk\tokenize\punkt.py", line 1357, in _realign_boundaries
for sl1, sl2 in _pair_iter(slices):
File "C:\Users\Rachel\AppData\Local\Programs\Python\Python38\lib\site-packages\nltk\tokenize\punkt.py", line 314, in _pair_iter
prev = next(it)
File "C:\Users\Rachel\AppData\Local\Programs\Python\Python38\lib\site-packages\nltk\tokenize\punkt.py", line 1330, in _slices_from_text
for match in self._lang_vars.period_context_re().finditer(text):
TypeError: expected string or bytes-like object
Any suggestions? I know word_tokenize deal with on record one time not for string, but I can not figure out whats wrong here. Thanks
Make sure there are no NaNs in that column
data.SAT_COMMENTS = data.SAT_COMMENTS.fillna('')

python urllib error - AttributeError: 'module' object has no attribute 'request'

I am trying out a tutorial code which fetches the html code form a website and prints it. I'm using python 3.4.0 on ubuntu. The code:
import urllib.request
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
text = page.read().decode("utf8")
print(text)
I saw previous solutions and tried them, I also tried importing only urllib but it still doesn't work. The error message displayed is as shown:
Traceback (most recent call last):
File "string.py", line 1, in <module>
import urllib.request
File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.4/email/message.py", line 15, in <module>
from email import utils
File "/usr/lib/python3.4/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/usr/lib/python3.4/email/charset.py", line 15, in <module>
import email.quoprimime
File "/usr/lib/python3.4/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
File "/media/saiwal/D89602199601F930/Documents/Copy/codes/python/headfirst/string.py", line 2, in <module>
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
AttributeError: 'module' object has no attribute 'request'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 21, in <module>
from urllib.request import urlopen
File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.4/email/message.py", line 15, in <module>
from email import utils
File "/usr/lib/python3.4/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/usr/lib/python3.4/email/charset.py", line 15, in <module>
import email.quoprimime
File "/usr/lib/python3.4/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
File "/media/saiwal/D89602199601F930/Documents/Copy/codes/python/headfirst/string.py", line 2, in <module>
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
AttributeError: 'module' object has no attribute 'request'
Original exception was:
Traceback (most recent call last):
File "string.py", line 1, in <module>
import urllib.request
File "/usr/lib/python3.4/urllib/request.py", line 88, in <module>
import http.client
File "/usr/lib/python3.4/http/client.py", line 69, in <module>
import email.parser
File "/usr/lib/python3.4/email/parser.py", line 12, in <module>
from email.feedparser import FeedParser, BytesFeedParser
File "/usr/lib/python3.4/email/feedparser.py", line 27, in <module>
from email import message
File "/usr/lib/python3.4/email/message.py", line 15, in <module>
from email import utils
File "/usr/lib/python3.4/email/utils.py", line 40, in <module>
from email.charset import Charset
File "/usr/lib/python3.4/email/charset.py", line 15, in <module>
import email.quoprimime
File "/usr/lib/python3.4/email/quoprimime.py", line 44, in <module>
from string import ascii_letters, digits, hexdigits
File "/media/saiwal/D89602199601F930/Documents/Copy/codes/python/headfirst/string.py", line 2, in <module>
page = urllib.request.urlopen("http://www.brainjar.com/java/host/test.html")
AttributeError: 'module' object has no attribute 'request'
This looks like a nasty coincidence.
TL;DR: Don’t name your script string.py.
So what’s happening here?
You’re trying to import urllib.request.
urllib.request tries to import http.client, which tries to import email.parser, which tries to import email.feedparser, which tries to import email.message, which tries to import email.utils, which tries to import email.charset, which tries to import email.quoprimime.
email.quoprimime tries to import string, expecting it to be the standard Python string module—but since the current working directory has priority over the standard Python library directories, it finds your string.py instead and tries to import that.
When importing your string.py, you try to import urllib.request. Since urllib.request is still being imported, you get back a skeleton urllib without a request attribute yet.
Because your imported string.py then fails because it can’t find the request attribute, the exception starts propagating back up.
But wait, there’s more! Since there was an error during an import, Ubuntu tries to be helpful by seeing if you’re missing a dpkg package. If so, it could say “hey, it looks like you’re missing this module; want to apt-get it?” So the mechanism for looking up the appropriate package is activated…
…but the module for looking up the appropriate package itself depends on urllib.request, so it tries to import it, and again fails…
In short, because you picked string.py as a file name, you overrode the standard string module, which broke a lot of other modules, and even broke the module that was supposed to be helpful when you were missing a module, causing a whole lot of havoc. Fortunately the solution is easy: rename your script.

openerp 7 start message "no module named pooler"

Error message as below when start, pls help,thanks
Traceback (most recent call last): File "/usr/local/bin/openerp-server", line 5, in
openerp.cli.main()
File
"/usr/local/lib/python2.7/site-packages/openerp-7.0_20130923_231151-py2.7.egg/openerp/cli/init.py",
line 51, in main
import(m)
File
"/usr/local/lib/python2.7/site-packages/openerp-7.0_20130923_231151-py2.7.egg/openerp/modules/module.py",
line 133, in load_module
mod = imp.load_module('openerp.addons.' + module_part, f, path, descr)
File
"/usr/local/lib/python2.7/site-packages/openerp-7.0_20130923_231151-py2.7.egg/openerp/addons/account_test/init.py",
line 1, in
import account_test
File
"/usr/local/lib/python2.7/site-packages/openerp-7.0_20130923_231151-py2.7.egg/openerp/addons/account_test/account_test.py",
line 32, in
import pooler
ImportError: No module named pooler
It looks like you are creating new module, and in that you have importd the pooler.
as per the solution change the import statement
import pooler
to
from openerp import pooler
in line 32 of account_test.py.