简体   繁体   English

如何使用 Python 从文件中读取文本?

[英]How to read text from a file using Python?

I have some header files in C that I want to read in Python.我有一些 C 语言的头文件,我想用 Python 读取它们。 Is there a way to do this?有没有办法做到这一点? For example, if I have a header file that contains the following code in C:例如,如果我有一个包含以下 C 代码的头文件:

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

Would I be able to extract this data using Python?我可以使用 Python 提取这些数据吗?

data = dict()
with open("file.h", "r") as f:
    for l in f:
        l = l.split(" ")
        data[l[1]] = l[2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM