User defined file name with pickle - python

I am using pickle to store variables as:
pickle.dump(database,open("save.p", "wb"))
Here database is the variable (type dictionary) I am trying to store and save.p is the destination file.
Now I want to replace this as
pickle.dump(database,open("%s"%var, "wb"))
Here var is taken from stdin. Basically letting the user specify the file.
However just writing this does not do the trick.
The pickle documentation suggests that a new file for the given name will be created if not found in the specified path.
What I have tried:
pickle.dump(database,open(var, "wb"))
to see if it can access the variable directly and pickle.dump(database,open("%s", "wb")%var).
I am missing something very obvious here. But can't figure out what.
EDIT:
I suppose there was some error with permissions. I now create the file with write permissions before running the script and it works. Thank you everyone.

Related

Opening a .docx file in S3 bucket in Python (Boto3)

In one of our S3 buckets, we have a .docx file with Mail Merge fields in it.
What I'm trying to do is directly read it directly from the bucket without first downloading it locally!
Typically, I can open a file and see the mail merge fields within it through the use of this code:
from mailmerge import MailMerge
document = MailMerge(r'C:\Users\User\Desktop\MailMergeFile.docx') # Trying to get a variable to pass in here
print(document.get_merge_fields())
As seen above, what I'm trying to do is to get the object in a way where I can just pass it to the MailMerge method, as though I were passing a path on my local machine.
The ways I've looked up to do this haven't been able to work.
fileobj = s3.get_object(
Bucket='bucketname',
Key='folder/mailmergefile.docx'
)
word_file = fileobj['Body'].read()
contents = word_file.decode('ISO-8859-1') # can't use utf-8 as that gives encoding error
contents
But then when I try and pass the contents variable to the Mailmerge function, I get another error:
document = MailMerge(contents)
print(document.get_merge_fields())
The error I get is:
ValueError: embedded null character
I presume you are using docx-mailmerge ยท PyPI.
The documentation is quite sparse but is shows MailMerge('input.docx'), which suggests that it is expecting the name of a file, not the 'contents' of a file.
In looking at the code, it seems to be calling a library to open a zip file.
Bottom line: As written, it wants the name of a file, not the contents of the file.

Python in SPSS - KEEP variables

I have selected the variables I need based on a string within the variable name. I'm not sure how to keep only these variables from my SPSS file.
begin program.
import spss,spssaux
spssaux.OpenDataFile(r'XXXX.sav')
target_string = 'qb2'
variables = [var for var in spssaux.GetVariableNamesList() if target_string in var]
vars = spssaux.VariableDict().expand(variables)
nvars=len(vars)
for i in range(nvars):
print vars[i]
spss.Submit(r"""
SAVE OUTFILE='XXXX_reduced.sav'.
ADD FILES FILE=* /KEEP \n %s.
""" %(vars))
end program.
The list of variables that it prints out is correct, but it's falling over trying to KEEP them. I'm guessing it's something to do with not activating a dataset or bringing in the file again as to why there's errors?
Have you tried reversing the order of the SAVE OUTFILE and ADD FILES commands? I haven't run this in SPSS via Python, but in standard SPSS, your syntax will write the file to disk, and then select the variables for the active version in memory--so if you later access the saved file, it will be the version before you selected variables.
If that doesn't work, can you explain what you mean by falling over trying to KEEP them?
It appears that the problem has been solved, but I would like to point out another solution that can be done without writing any Python code. The extension command SPSSINC SELECT VARIABLES defines a macro based on properties of the variables. This can be used in the ADD FILES command.
SPSSINC SELECT VARIABLES MACRONAME="!selected"
/PROPERTIES PATTERN = ".*qb2".
ADD FILES /FILE=* /KEEP !selected.
The SELECT VARIABLES command is actually implemented in Python. Its selection criteria can also include other metadata such as type and measurement level.
You'll want to use the ADD FILES FILE command before the SAVE for your saved file to be the "reduced" file
I think your very last line in the python program should be trying to join the elements in the list vars. For example: %( " ".join(vars) )

Retrieve and Rank in Python-Configuration File Type?

I'm using the retrieve_and_rank class in Python from the watson_developer_cloud package.
Main question:
In the create_config function, what type of object do we pass in for the config argument? A zip folder?
I'm using IBM's provided example configuration folder just to test, and when I call
retriever_and_ranker.create_config(MY_ID, CONFIG_NAME, 'blank_example_solr_config.zip')
I get this error:
WatsonException: unknown error
The zipped folder is in the same directory as my code. What should that last argument be? I'm wondering why it doesn't recognize the zip file from the string I'm passing in.
I realized the key was that I was incorrectly passing in a string, as opposed to an open file object.
You need to use the
with open("file.zip", "rb) as fp:
...
as in joe's answer.

Python: storing and using information created in a function

I just began with Python and am having a little difficulty with storing the result of a function in a variable.
I have a small script that does the following:
change to a directory and within that directory:
create a new directory named to the moment it has been created (for example 2016200420161636)
what i want it to do additionally:
create a file within that newly created directory
I would think to be able to have the file created in the newly created directory I need to store the directory name ( 2016200420161636) and return the value to a part of the script that creates the file (so it knows where to write the file to).
Can someone please advise?
Are you looking to just save the value before you create the directory?
something like
timestamp = time.clock()
if not os.path.exists(timestamp)
os.makedirs(timestamp)
now timestamp has the value stored and you can use as needed

How to permanently update a path variable in a python script

I'd like to know if there's a way to dynamically edit the lines in my scripts depending on a user input from an arcpy tool that I have created. the interface will allow the user to pick a file, then there's a boolean check to permanently change the default directory if the program was run later.
if you are familiar with arcpy:
def example():
defaultPath="C:\\database.dbf"
path=arcpy.GetParameterAsText(0)#returns a string of a directory
userCheck=arcpy.GetParameterAsText(1)#returns a string "TRUE"/"FALSE"
if path=="":
path=defaultPath
The goal here is to change "defaultPath" permanently if "userCheck" is "TRUE". I think it's possible to do that with the use of classes? or do I need to have an "index" table and refer to it's cells (like an excel sheet).
Thanks

Categories

Resources