-3

Need a help on creating/generating .py file from a xls file.

I got a requirement where user provides configuration data in xls file and I need to invoke a external tool which takes python file as input and do configuration. So I need to develop a application in python where xls data should be converted to python dictionaries.

So each row in xls makes up a python dict. xls may contains multiple worksheet also. How can I achieve this requirement using python.

  • 2
    Too vague title. Show your effort? – user202729 Jan 21 '18 at 10:28
  • you can see the answer in https://stackoverflow.com/questions/14196013/python-creating-dictionary-from-excel-data – xu he Jan 21 '18 at 10:56
  • @xuhe I already written functions to read the data from xls and create python dict's but the problem here is writing to a python file with indentation followed. Dumping data to a csv or json can be done using API's. But how to create .py with dict's and list's ?? – Vijaysachin Jan 21 '18 at 14:22
  • If you're already able to extract data from the CSV you're really only interested in saving, which is a very well-trodden question. [Here's an example](https://pythonprogramming.net/python-pickle-module-save-objects-serialization/) from PythonProgramming that covers Pickle in some detail, to go alongside Xu's answer. – neophlegm Jan 23 '18 at 03:07
  • @Vijaysachin, ...why would any intelligently-designed tool use `.py` files as a data format? Unlike better-designed formats, it's *inherently* Turing-complete and allows arbitrary code execution, making it bad for security; it's much slower to parse than formats built for speed, and much larger than formats built for size. – Charles Duffy Jan 23 '18 at 03:30
  • ...regardless of whether it's a good idea or not, though, this question is simply too vague to answer as currently asked. It would do a great deal of good if you provided specific sample of the output you want to generate, along with the input corresponding with same. It'd be even more useful than that if you actually tried to build an implementation yourself, and asked a narrow, tightly scoped question about the specific place where you got stuck. – Charles Duffy Jan 23 '18 at 03:31

1 Answers1

0

I am not sure if I understand your question. If you mean the form of data you read from csv is different from former form. For example, the data you saved is dict but when you read it, you get array or something else. Then you can use pickle package. There is a simple sample about how to save and read dict.

import pickle 
dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}
f1 = open("test.txt","wb") 
pickle.dump(dict, f1) # save dict
f1.close() 
f2 = open("test.txt","rb") 
load_list = pickle.load(f2) # load dict
f2.close()
print(load_list)
xu he
  • 1
  • If a question is not clear enough to understand (and, importantly, clear enough *for someone other than the OP to be able to tell if an answer is correct*), it's not yet clear enough to answer. See [How to Answer](https://stackoverflow.com/help/how-to-answer) in the Help Center, particularly the section "Answer Well-Asked Questions", particularly the bullet regarding questions which "are unclear or lacking specific details that can uniquely identify the problem". – Charles Duffy Jan 23 '18 at 03:27
  • (Moreover, in the case at hand, the OP is asking how to use `.xls` -- Excel spreadsheets -- as input; Pickle files are not xls). – Charles Duffy Jan 23 '18 at 03:28