从文本文件中读取Python代码 [英] Reading Python code from text files

查看:226
本文介绍了从文本文件中读取Python代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文本文件的内容用作Python代码


,例如如果我在文本文件中写入


import sys


,我可以将其作为字符串读取然后将其转换为Python代码或者是有另一种方法吗?


谢谢

I am trying to use the contents of a text file as Python code

e.g. if I write

import sys

in a text file, can I read that as a string and then turn it into Python code or is there another way to do it?

thanks

推荐答案


I我试图使用文本文件的内容作为Python代码


例如如果我在文本文件中写入


import sys


,我可以将其作为字符串读取然后将其转换为Python代码或者是有另一种方法吗?


谢谢
I am trying to use the contents of a text file as Python code

e.g. if I write

import sys

in a text file, can I read that as a string and then turn it into Python code or is there another way to do it?

thanks



Python脚本(.py& .pyw)文件是文本文件。它们由编译器读取,转换为由执行引擎运行的字节代码(.pyc)文件。这个过程的所有部分都暴露在库模块中。

Python script (.py & .pyw) files ARE text files. They are read by the compiler an turned into byte code (.pyc) files which are run by the execution engine. All parts of this process are exposed in the library modules.


oops对不起我想我不是太精确了。


我的意思是,如果我编写一个打开文本文件并读取内容的Python程序,我想使用该文本文件的内容来访问一些常量。


例如我已经定义了50多个常量,当我读完我的Python程序打开的文本文件并找到一个匹配我的常量名称的字符串时,我想使用该常量。


所以在我的剧本中,我写了


porta = 100


和当我在文本文件中看到''porta''我想要


打印porta


我尝试使用if语句但是有50+其中我不认为那是最好的选择。


如果这仍然让人感到困惑,我很抱歉。我还是很陌生。


欢呼
oops sorry I guess I wasn''t too precise.

What I mean is, if I write a Python program that opens a text file and reads through the contents, I want to use the contents of that text file to access some constants.

e.g. I have defined more than 50 constants and when I read through the text file opened by my Python programme and find a string matching the name of one of my constant, I want to use that constant.

so in my script, I''ve written

porta = 100

and when i see ''porta'' in the text file i want to

print porta

I tried using if statements but with 50+ of them I don''t think that that''s the best option.

I''m sorry if this still sounds confusing. I''m still really new to this.

cheers



o抱歉我猜我不太精确。


我的意思是,如果我编写一个打开文本文件并读取内容的Python程序,我想使用它的内容文本文件来访问一些常量。


例如我已经定义了50多个常量,当我读完我的Python程序打开的文本文件并找到一个匹配我的常量名称的字符串时,我想使用该常量。


所以在我的剧本中,我写了


porta = 100


和当我在文本文件中看到''porta''我想要


打印porta


我尝试使用if语句但是有50+其中我不认为那是最好的选择。


如果这仍然让人感到困惑,我很抱歉。我还是很陌生。


欢呼
oops sorry I guess I wasn''t too precise.

What I mean is, if I write a Python program that opens a text file and reads through the contents, I want to use the contents of that text file to access some constants.

e.g. I have defined more than 50 constants and when I read through the text file opened by my Python programme and find a string matching the name of one of my constant, I want to use that constant.

so in my script, I''ve written

porta = 100

and when i see ''porta'' in the text file i want to

print porta

I tried using if statements but with 50+ of them I don''t think that that''s the best option.

I''m sorry if this still sounds confusing. I''m still really new to this.

cheers



这很容易实现:

This can be accomplished quite easily:

展开 | 选择 | Wrap | 行号


这篇关于从文本文件中读取Python代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆