0

I have some header files in C that I want to read in Python. Is there a way to do this? For example, if I have a header file that contains the following code in C:

#define x 0x00
#define y 0x03
#define z 0x0003

Would I be able to extract this data using Python?

2
  • So, you just want to read the raw text from the .h files? If so, there is no difference in reading a .h from a .txt or whatever. Just reading text from a file. If you want to actually parse the code or do some kind of C interop from Python, the procedure is a bit different! Commented Jun 24, 2021 at 1:01
  • I don't get what you're trying to do here. You mean you want to get back a mapping as in converting hex numbers? Is it ntohs(0x0003) as in Capture all packets in some socket protocol? Is it the Nvidia GeForce error codes or what?? More info needed. Commented Jun 24, 2021 at 1:55

1 Answer 1

1
data = dict()
with open("file.h", "r") as f:
    for l in f:
        l = l.split(" ")
        data[l[1]] = l[2]
Sign up to request clarification or add additional context in comments.

3 Comments

Many ways to achieve that. Easiest is probably using a full path in the first argument to open(), like open('/path/to/file/here.h', 'r').
If you use the code from @Programmer the text from the file is read into a dict structure, so you'd have to traverse the dict to get to the text elements. It isn't supposed to "open" an application containing the text / document, if that's what you mean?
If that is what you want, then a dirty trick to open the system defined app for the given file-type could be: import webbrowser webbrowser.open('/path/to/file.txt')

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.