I am trying to make a dictionary with pickle by using command line but while getting input from command line I am getting "module object has no attribute load " ?
Here's my code:
import pickle
import sys
dfile = open ("new.dat","w")
print "get argument"
lifesize=(sys.argv[1])
print "get another argument"
two=sys.argv[2]
print "last argument"
three=sys.argv[3]
z={lifesize:[two,three]}
pickle.dump(z,dfile)
dfile.close()
ifile=open("new.dat")
d1= pickle.load(ifile)
and save above as newdocument
cmd:python newdocument.py
I also tried to do a dictionary for every lifesize and save them as new.dat and get them..
need really help .?thank you
The code you posted is perfectly fine, please check your version of Python, and possibly update/reinstall.
The Python interpreter complains that pickle.load() doesn't exist, while it certainly does: http://docs.python.org/library/pickle.html#pickle.load
Related
import json
f=open("99_jiayi.txt",'r',encoding = 'utf-8-sig')
a=json.load(f)
f.close()
https://i.stack.imgur.com/vD3M5.png
https://i.stack.imgur.com/QP0oW.png
I tried to turn the text file into a list in order to analyze the data
I used the "json load" and it worked on another file which is written in the same mode
But when i want to use it on another file it comes out the error
i searched google for a lot of time but still cant get the ans
Hope someone can help me with this question
i have some problem to express my thought with eng so if anyone cant understand what i am typing plz let me know tks!!
The error message points to "1":NR, which does not look like valid JSON, so it seems like a valid error.
Edit: try putting all NR within quotes.
I'm trying to import another python script with a name of the form fe_fi_fo_fam.py, I'm using importlib.import_module("fe_fi_fo_fam.py") but I'm getting the error : "module fe not found" , how do I get it to read the whole string instead of only the first part? I'm new to python and I seem to have scoured the internet without an answer.
Yo can instead use the built-in function exec after you compose the module string
>>> modOs = 'os'
>>> exec('import ' + modOs)
>>> os.getcwd()
'C:\\Desktop'
Note: Python 3.6 exec
I think you have to understand the difference between module name in Python and file name. importlib.import_module() gets a "module name", not a "file name", so "fe_fi_fo_fam.py" is not a valid argument, instead you have to use "fe_fi_fo_fam" with no .py extension.
Here's my first simple test program in Python. Without importing the os library the program runs fine... Leading me to believe there's something wrong with my import statement, however this is the only way i ever see them written. Why am I still getting a syntax error?
import os # <-- why does this line give me a syntax error?!?!?! <unicode error> -->
CalibrationData = r'C:/Users/user/Desktop/blah Redesign/Data/attempts at data gathering/CalibrationData.txt'
File = open(CalibrationData, 'w')
File.write('Test')
File.close()
My end goal is to write a simple program that will look through a directory and tabularize data from relevant .ini files within it.
Well, as MDurant pointed out... I pasted in some unprintable character - probably when i entered the URL.
I have the following python script which i want to run..
However, it keeps showing the error message on my command prompt whenever i attempt to run the script.
Error message:
File "xor.py", line 9
File = open(sys.argv[1], 'rb').read<>
SyntaxError: Invalid Syntax
The following is the command i executed in cmd:
python xor.py sample_output.txt 'what would the secret be?'
The following is the script:
# xor.py
import sys
from itertools import cycle
file = open(sys.argv[1], 'rb').read()
string = sys.argv[2]
sys.stdout.write(''.join(chr(ord(x)^ord(y)) for (x,y) in zip(file, cycle(string))))
You are not running the code you are editing, instead you are running a different file than the one you edited.
This is because there is no syntax error in the code that you have provided. However, there is a syntax error in the code in the error message:
File = open(sys.argv[1], 'rb').read<>
This ends with <>, not with (). I assumed this to be a transcription error, but you say that the error message really appears like this, although the code does not.
Hence: You are running a different file than the one you are editing.
You have .read<> when you probably intended .read()
First of all, "file" is already reserved; that is built-in keyword so unable to set as the name of variable.
And second, do not use <> instead of (). incorrect in grammar.
The problems might be clearly solved if you code like:
fd = open(sys.argv[1], 'rb').read()
here's the code I am using
import os
import decimal
from pyPdf import PdfFileReader
path = r"E:\python\Real Python\Real Python\Course materials\Chapter 8\Practice files"
inputFileName = os.path.join(path,"Pride and Prejudice.pdf")
inputFile = PdfFileReader(file(inputFileName,"rb"))
print "Number of pages:", inputFile.getNumPages()
print "Title:", inputFile.getDocumentInfo().title
Now, when I run this code I am getting an error:
'module' object has no attribute 'Number'
I took a screenshot of the whole output that I got
when I run the above code, with errors and everything.
so,please take a look and let me know what's wrong?
After I used import decimal in the code,
I got a few errors.
So, I took a screenshot of the whole thing and am attaching it here.
pyPdf is now renamed to pypdf and a couple of other classes / methods were renamed. I'm the maintainer of pypdf and PyPDF2.
Another change was that we dropped using decimal. Instead, we now use the built-in float. Hence this error will not occur in later versions of pypdf.