[SOLVED] Python Results Converted To C Struct Header File
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Maybe I am missing the point, but this is how I would do it. And, well, have actually done it! So, to reiterate; use the output from Python program 1 as input to Python program 2. Or even better, why don't you write a single Python program that does everything at once. Seems the most efficient to me, but I don't have the whole picture.
HMW, thanks for your reply. It was suggested to me basically what you are saying. So essentially just treat it as regular Python output and just redirect the output to a regular file (ex. versions.h) so it can be used by the existing C code in place.
HMW, thanks for your reply. It was suggested to me basically what you are saying. So essentially just treat it as regular Python output and just redirect the output to a regular file (ex. versions.h) so it can be used by the existing C code in place.
Yeah, it makes sense to me. Holler if you want some assistance!
ummm ... don't we need to see the 'blah blah blah' if we to know how you got the data in the correct format? I mean the bit you have shown is nice but is essentially just a list of print statements.
I opted for saving the static information in a variable instead. Thus you don't have to write more than once (or print in my case).
Here's my approach:
Code:
#!/usr/bin/env python3
out = """struct {
char *name;
char *fixed_version;
} filename_versions[] = {
"""
with open("infile.txt") as infile:
for line in infile:
line = line.rstrip()
lineSplit = line.split("=")
out += "{\"" + lineSplit[0] + "\",\"" + lineSplit[1] + "\"},\n"
out = out[:-1] # Remove the unwanted \n from the final loop
out += """
{NULL, NULL}
};
"""
print(out)
exit(0)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.