Pyexcel, how to save a dict to csv file with pyexcel? - python

This part of the topic is solved, see my last answer to this topic.
I'm having trouble to save a dictionary using the pyexcel module.
I instaled the pyxecel module with pip3 install pyexcel
So I have a dictionary and I'm doing this:
import pyexcel as pe
myDict = {...}
pe.save_as(dict = myDict, dest_file_name = "dest_file.xls")
In my terminal, when I compile the code, it prints myDict but just after I have this:
Otherwise unrecognized parameters were given.
terminal output
I look for a solution and it could be related to this: https://pyexcel-io.readthedocs.io/en/latest/pyinstaller.html
But I don't understand how to use the --hiden-import in my code or terminal? When I use this in my code I have a syntax error.
Can someone help me, please?
Thank you in advance.
edit: use pe.save_as(adict = myDict, dest_file_name = "dest_file.xls") to solve the problem.

The reason that this is failing is because the method call save_as() doesn't accept a parameter named dict. According to the documentation, providing a dict should either be done via the adict or with_keys parameters:
pe.save_as(adict = myDict, dest_file_name = "dest_file.xls")

Related

music21: get keys of midi file?

I'm trying to analyze a midi file with music21 to get the keys of that file. Does anyone know the command for that or where to find an example for this?
I'm new to this.
Thank you a lot in advance.
Assuming you need to analyze with a key-finding algorithm (as opposed to just reading the key signature provided by the encoder, if present), then create a music21.stream.Score and call analyze("key"):
my_score: music21.stream.Score = music21.converter.parse('path.mid')
k = my_score.analyze('key')
print(k.name)
Some other fun stuff like alternateInterpretations and correlationCoefficient are described in the User's Guide. Enjoy!

Parameter with dictionary path

I am very new to Python and am not very familiar with the data structures in Python.
I am writing an automatic JSON parser in Python, the JSON message is read into a dictionary using Ultra-JSON:
jsonObjs = ujson.loads(data)
Now, if I try something like:
jsonObjs[param1][0][param2] it works fine
However, I need to get the path from an external source (I read it from the DB), we initially thought we'll just write in the DB:
myPath = [param1][0][param2]
and then try to access:
jsonObjs[myPath]
But after a couple of failures I realized I'm trying to access:
jsonObjs[[param1][0][param2]]
Is there a way to fix this without parsing myPath?
Many thanks for your help and advice
Store the keys in a format that preserves type information, e.g. JSON, and then use reduce() to perform recursive accesses on the structure.

Open URL stored in a csv file

I'm almost an absolute beginner in Python, but I am asked to manage some difficult task. I have read many tutorials and found some very useful tips on this website, but I think that this question was not asked until now, or at least in the way I tried it in the search engine.
I have managed to write some url in a csv file. Now I would like to write a script able to open this file, to open the urls, and write their content in a dictionary. But I have failed : my script can print these addresses, but cannot process the file.
Interestingly, my script dit not send the same error message each time. Here the last : req.timeout = timeout
AttributeError: 'list' object has no attribute 'timeout'
So I think my script faces several problems :
1- is my method to open url the right one ?
2 - and what is wrong in the way I build the dictionnary ?
Here is my attempt below. Thanks in advance to those who would help me !
import csv
import urllib
dict = {}
test = csv.reader(open("read.csv","rb"))
for z in test:
sock = urllib.urlopen(z)
source = sock.read()
dict[z] = source
sock.close()
print dict
First thing, don't shadow built-ins. Rename your dictionary to something else as dict is used to create new dictionaries.
Secondly, the csv reader creates a list per line that would contain all the columns. Either reference the column explicitly by urllib.urlopen(z[0]) # First column in the line or open the file with a normal open() and iterate through it.
Apart from that, it works for me.

How to download a file using Python

I tried to download something from the Internet using Python, I am using urllib.retriever from the urllib module but I just can't get it work. I would like to be able to save the downloaded file to a location of my choice.
If someone could explain to me how to do it with clear examples, that would be VERY appreciated.
I suggest using urllib2 like so:
source = urllib2.urlopen("http://someUrl.com/somePage.html").read()
open("/path/to/someFile", "wb").write(source)
You could even shorten it to (although, you wouldnt want to shorten it if you plan to enclose each individual call in a try - except):
open("/path/to/someFile", "wb").write(urllib2.urlopen("http://someUrl.com/somePage.html").read())
You can also use the urllib:
source = urllib.request.urlopen(("full_url")).read()
and then use what chown used above:
open("/path/to/someFile", "wb").write(source)

Python Advice for a beginner. Regex, Dictionaries etc?

I'm writing my second python script to try and parse the contents of a config file and would like some noob advice. I'm not sure if its best to use regex to parse my script since its multiple lines? I've also been reading about dictionaries and wondered if this would be good practice. I'm not necessarily looking for the code just a push in the right direction.
Example: My config file looks like this.
Job {
Name = "host.domain.com-foo"
Client = host.domain.com-fd
JobDefs = "DefaultJob"
FileSet = "local"
Write Bootstrap = "/etc/foo/host.domain.com-foo.bsr"
Pool = storage-disk1
}
Should I used regex, line splitting or maybe a module? If I had multiple jobs in my config file would I use a dictionary to correlate a job to a pool?
If you can change the configuration file format, you can directly write your file as a Python file.
config.py
job = {
'Name' : "host.domain.com-foo",
'Client' : "host.domain.com-fd",
'JobDefs' : "DefaultJob",
'FileSet' : "local",
'Write Bootstrap' : "/etc/foo/host.domain.com-foo.bsr",
'Pool' : 'storage-disk1'
}
yourscript.py
from config import job
print job['Name']
There are numorous existing alternatives for this task, json, pickle and yaml to name 3. Unless you really want to implement this yourself, you should use one of these. Even if you do roll your own, following the format of one of the above is still a good idea.
Also, it's a much better idea to use a parser/generator or similar tool to do the parsing, regex's are going to be harder to maintain and more inefficient for this type of task.
If your config file can be turned into a python file, just make it a dictionary and import the module.
Job = { "Name" : "host.domain.com-foo",
"Client" : "host.domain.com-fd",
"JobDefs" : "DefaultJob",
"FileSet" : "local",
"Write BootStrap" : "/etc/foo/host.domain.com-foo.bsr",
"Pool" : "storage-disk1" }
You can access the options by simply calling Job["Name"]..etc.
The ConfigParser is easy to use as well. You can create a text file that looks like this:
[Job]
Name=host.domain.com-foo
Client=host.domain.com-fd
JobDefs=DefaultJob
FileSet=local
Write BootStrap=/etc/foo/host.domain.com-foo.bsr
Pool=storage-disk1
Just keep it simple like one of the above.
ConfigParser module from the standard library is probably the most Pythonic and staight-forward way to parse a configuration file that your python script is using.
If you are restricted to using the particular format you have outlined, then using pyparsing is pretty good.
I don't think a regex is adequate for parsing something like this. You could look at a true parser, such as pyparsing. Or if the file format is within your control, you might consider XML. There are standard Python libraries for parsing that.

Categories

Resources