I am trying to load a simple list from a file to another, but python raises _pickle.UnpicklingError: could not find MARK as soon as I run it. The code is really simple and follows what the course tells me, I really don't understand. It goes as follows: file "donnees.py" has the list, and "fonctions.py" has the rest.
donnees.py
listemots=["bonjour","pivers","cactus","france","taureau","espace"]
fonctions.py
import pickle
import random
with open("donnees.py","rb") as donnees:
unpickler1=pickle.Unpickler(donnees)
listerecuperee=unpickler1.load()
print(listerecuperee)
Error raises is:
Traceback (most recent call last):
File "/Users/sebastienchabrol/Documents/Cours de python/pendu/fonctions.py", line 6, in <module>
listerecuperee=unpickler1.load()
_pickle.UnpicklingError: could not find MARK
Does someone have an idea about how to fix this ? Many thanks !!
For this just use:
from donnees import listemotes
Don’t use .py files to pickle variables. If you want to persist a list then use the csv module.
Related
I am trying to fetch the free disk space details of each drive using Python Scripts and storing the values in a variable 'p'.
p=(disk_usage('C:/'))
f=open('C:\\Python27\\solr.log','a')
f.write("Solr "+p)
I would like to place the results from variable 'p' into a log file. If I print the variable 'p', I am able to get the results successfully. But when I try to open a log file and write into it, it is throwing the below error.
"Traceback (most recent call last):
File "C:\Python27\sample.py", line 51, in <module>
f.write("Result: "+p)
TypeError: must be str, not UsageTuple"
Could anyone please help me on this.
I got the answer. Just changed the 'p' variable to string and then I tried to write into file. I am able to get the results in my log file.
I'm currently trying to convert the .binaryproto file here to a usable numpy array. I'm running everything in my python terminal and following some of the guides as given here.
I can make it as far as seen below:
import caffe
blob = caffe.proto.caffe_pb2.BlobProto()
data = open('ucf101_train_mean.binaryproto','rb').read()
blob.ParseFromString(data)
At which point I get the error:
Traceback (most recent call last)L
File "<stdin>", line 1, in <module>
google.protobuf.message.DecodeError: Error parsing message
I've cleared and reinstalled caffe thinking it was an installation problem and it hasn't helped. I printed the data string and checked the length and both seem appropriate.
Or, as an alternative solution - is there another way I could potentially load the values of the .binaryproto file to get a usable mean? Thank you!
I am receiving the following error message when I try to read a Vensim model file (.mdl) using Python's PySD package.
My code is:
import pysd
import os
os.chdir('path/to/model_file')
model = pysd.read_vensim('my_model.mdl')
The Error I receive is:
Traceback (most recent call last):
Python Shell, prompt 13, line 1
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pysd/pysd.py", line 53, in read_vensim
py_model_file = translate_vensim(mdl_file)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pysd/vensim2py.py", line 673, in translate_vensim
entry.update(get_equation_components(entry['eqn']))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pysd/vensim2py.py", line 251, in get_equation_components
tree = parser.parse(equation_str)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parsimonious/grammar.py", line 123, in parse
return self.default_rule.parse(text, pos=pos)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parsimonious/expressions.py", line 110, in parse
node = self.match(text, pos=pos)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/parsimonious/expressions.py", line 127, in match
raise error
parsimonious.exceptions.ParseError: Rule 'subscriptlist' didn't match at '' (line 1, column 21).
I have searched for this particular error and I cannot find much information on the failed matching rule for 'subscriptlist'.
I appreciate any insight. Thank you.
Good news is that there is nothing wrong with your code. =) (Although you can also just include the path to the file in the .read_vensim call, if you don't want to make the dir change).
That being the case, there are a few possibilities that would cause this issue. One is if the model file is created with a sufficiently old version of Vensim, the syntax may be different from what the current parser is designed for. One way to get around this is to update Vensim and reload the model file there - Vensim will update to the current syntax.
If you are already using a recent version of Vensim (the parser was developed using syntax of Vensim 6.3E) then the parsing error may be due to a feature that isn't yet included. There are still some outstanding issues with subscripts, which you can read about here and here).
If you aren't using subscripts, you may have found a bug in the parser. If so, best course is to create a report in the github issue tracker for the project. The stack trace you posted says that the error is happening in the first line of the file, and that the error has to do with how the right hand side of the equation is being parsed. You might include the first few lines in your bug report to help me recreate the issue. I'll add a case to our growing test suite and then we can make sure it isn't a problem going forwards.
I'm newbie here and I wouldn't want to ask such a easy question as my first post but I don't know anything about Python even I'm a PHP/C programmer.
I have a python script in Figway tools which is called RegisterDevice.py to register my own sensor hardware to FIWARE Lab. But some code lines of that script doesn't work as I expected because of Python3.4. This may not be my problem but I don't have too much time to wait an official solution that's why I thought that I could resolve it as a person who is familiar to the programming.
I've searched on the web for solution but I couldn't find any exact solution for it yet. As far as I read bytes and unicode strings are two different types in Python3.x but I couldn't realize where I have to encode or maybe decode string to other type on the code. Maybe I have to do something else...
Here is the part of script which gave me error like above.
# Load the configuration file
with open(CONFIG_FILE,'r+') as f:
sample_config = f.read()
#config = ConfigParser.RawConfigParser(allow_no_value=True)
config = configparser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))
Error:
Traceback (most recent call last):
File "RegisterDevice.py", line 47, in <module>
config.readfp(io.BytesIO(sample_config))
TypeError: 'str' does not support the buffer interface
Firstly readfp() is deprecated in Python3 and you should use read_file().
The best way is probably using the read() function directly when you want to work with a file. You should set encoding as the second parameter if you expect non-ASCII characters inside the file.
The alternative is to read_string() and give it a string directly.
I have been doing work very similar to this, and I believe this script runs, but you will have to verify if it gives you the desired results:
import configparser
with open('.coveragerc','r+') as f:
#config = ConfigParser.RawConfigParser(allow_no_value=True)
config = configparser.RawConfigParser(allow_no_value=True)
config.readfp(f)
I'm trying to get a recipe working that I found online for doing expectation maximization (http://code.activestate.com/recipes/577735-expectation-maximization/). I run into the following error:
Traceback (most recent call last):
File "./runem.py", line 7, in <module>
print expectation_maximization([[1,2,3,4,5],[2,3,4,5,6],[9,8,7,4,1]], 2)
File "/local/scratch-3/dk427/rp/em.py", line 83, in expectation_maximization
Px[o,c] = pnorm(t[o,:], params[c]['mu'], params[c]['sigma'])
File "/local/scratch-3/dk427/rp/em.py", line 18, in pnorm
xmt = np.matrix(x-m).transpose()
TypeError: __array_prepare__ must return an ndarray or subclass thereof which is otherwise identical to its input
There must be some flaw in the algorithm, or I'm giving it the wrong input, but I can't find what's going wrong. I've found that the error is caused by the subtraction x-m, but x.dtype=int64 and m.dtype=float64, which I think should work.
Does anyone have any ideas?
You seem to be passing in a list of lists rather than an array. You could do something like:
ts = np.array([[1,2,3,4,5],[2,3,4,5,6],[9,8,7,4,1]])
expectation_maximization(ts, 2)
That seems to have some problems at some point with taking a square root on my computer, but I think that might be because that data isn't good for this algorithm for some reason (but I don't know what the algorithm is trying to do, so I can't say for sure).