Saving to external PY file? - python

I'm working on a online server, and I need all my list and dict saved. What would be the best and quickest way to approach this?
I tried importing the data, and it works to load the data. But how can I update the imported file?

I think you can use pickle/cPickle module to save and load the date, which are built-in module and easy to use.
I am not very sure the meaning of update import file, what about rewrite the content back to the file after updating in the program?

Related

Acess the JSON of a Jupyter Noteook While inside of it

I find that Jupyter notebooks are stored in .json format.
How can I access this (read only) while inside of the notebook itself?
I want to programmatically get the name of the notebook I am currently working in as a string.
EDIT:
Just want to clarify that I know of the solutions using ipynbname and ipyparams, and I am looking for alternate solutions. Thanks all.
for your particular use case you might want to use a package called ipyparams
You can import it at the top of your file and get your file's name as such:
import ipyparams
notebook_name = ipyparams.notebook_name

reading and storing data into a python data structure from a csv

i am trying to figure out how to read my survey data but i keep getting this error. i feel like i am missing some things when i type out how to specifically find the file i need... and would a dictionary be the best data structure to use for this? the ultimate goal is: by using each voting method to determine a winner from the data that was collected. i plan on using if/else if statements...
Did you check your directory properly? The error says No such file or directory. Provide the full correct dir for your csv file and it should work fine.
In addition to providing a full file path, you can put the csv file with the python script under the same folder and try.

Dynamic update of .py files' variables

I have a python program that loads preferences from another .py file.
And I need to implement a way to update this preferences on the way.
I've been using text files and reading it, and then, use a template, format it, save it and reading again to load the preferences variables. But when I add a new parameter I need to reformat and rewrite the functions that read, format and write.
So I think a better way to do this is with .py files to load. But I don't know if there's a way to modify this variables and save the .py file while using it.
Any idea or solution to this?

PySpark: spit out single file when writing instead of multiple part files

Is there a way to prevent PySpark from creating several small files when writing a DataFrame to JSON file?
If I run:
df.write.format('json').save('myfile.json')
or
df1.write.json('myfile.json')
it creates the folder named myfile and within it I find several small files named part-***, the HDFS way. Is it by any means possible to have it spit out a single file instead?
Well, the answer to your exact question is coalesce function. But as already mentioned it is not efficient at all as it will force one worker to fetch all data and write it sequentially.
df.coalesce(1).write.format('json').save('myfile.json')
P.S. Btw, the result file is not a valid json file. It is a file with a json object per line.
This was a better solution for me.
rdd.map(json.dumps)
.saveAsTextFile(json_lines_file_name)
df1.rdd.repartition(1).write.json('myfile.json')
Would be nice, but isn't available. Check this related question. https://stackoverflow.com/a/33311467/2843520

Writing data to a zip archive in Python

I've been told in the past that there is simply no easy way to write a string a zip file. It's okay to READ from a zip archive, but if you want to write to a zip file, the best option is to extract it, make the changes, and then zip it back up again. However, the library I am using (openpyxl) accomplishes the feat of writing to a zip file without any extraction. This package uses the writestr() function in the python ZipFile library to make changes. Can someone explain to me how exactly this is possible? I know it has something to do with writing bytes but I can't fine a good explanation.
I'm aware of the vagueness of this question, but that's a circumstance of my lack of knowledge on the topic.
openpyxl does not modify the files in place because you can't do this with zipfiles. You must extract, modify and archive. We just hide this process in the library.

Categories