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 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 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 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

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:

Expand|Select|Wrap|Line Numbers

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

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

import sys

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

谢谢

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

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

import sys

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

谢谢

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

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

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

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

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

porta = 100

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

打印porta

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

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

欢呼

o抱歉我猜我不太精确。

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

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

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

porta = 100

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

打印porta

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

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

欢呼

这很容易实现:

展开 | 选择 | Wrap | 行号

python读取txt文件代码-从文本文件中读取Python代码相关推荐

  1. python按列读取txt文件_如何使用pandas读取txt文件中指定的列(有无标题)

    最近在倒腾一个txt文件,因为文件太大,所以给切割成了好几个小的文件,只有第一个文件有标题,从第二个开始就没有标题了. 我的需求是取出指定的列的数据,踩了些坑给研究出来了. import pandas ...

  2. python读取txt文件并写入excel-Python实现读取txt文件并转换为excel的方法示例

    本文实例讲述了Python实现读取txt文件并转换为excel的方法.分享给大家供大家参考,具体如下: 这里的txt文件内容格式为: 892天平天国定都在?A开封B南京C北京(B) Python代码如 ...

  3. python读取txt文件的行数_python读取txt文件符合条件的行数-女性时尚流行美容健康娱乐mv-ida网...

    女性时尚流行美容健康娱乐mv-ida网 mvida时尚娱乐网 首页 美容 护肤 化妆技巧 发型 服饰 健康 情感 美体 美食 娱乐 明星八卦 首页 > 高级搜索 python 关系网图 pyth ...

  4. java io流读取txt文件_Java使用IO流读取TXT文件

    通过BufferedReader读取TXT文件 window系统默认的编码是GBK,而IDE的编码多数为UTF-8,如果没有规定new InputStreamReader(new FileInputS ...

  5. python求txt文件内平均值_如何使用python计算几个.dat文件的平均值?

    这是一种相当时间和资源有效的方法,它读取值并并行计算所有文件的平均值,但每次只读取一行文件 – 但它会暂时读取整个第一个.dat文件进入内存以确定每个文件中将有多少行和每列数字. 你没有说你的&quo ...

  6. Java 读取TXT文件的多种方式-行读取,字节读取

    Java 读取TXT文件的多种方式 1).按行读取TXT文件 package zc; import java.io.BufferedReader; import java.io.File; impor ...

  7. python怎么读取txt文件数据保存数组中-Python Numpy中数据的常用的保存与读取方法...

    在经常性读取大量的数值文件时(比如深度学习训练数据),可以考虑现将数据存储为Numpy格式,然后直接使用Numpy去读取,速度相比为转化前快很多. 下面就常用的保存数据到二进制文件和保存数据到文本文件 ...

  8. python怎么读取txt文件数据保存数组中-python将txt等文件中的数据读为numpy数组的方法...

    实际中,很多数据都是存为txt文件.csv文件等,但是在程序中处理的时候numpy数组或列表是最方便的.本文简单介绍读入txt文件以及将之转化为numpy数组或列表的方法. 1 将txt文件读为lis ...

  9. python分析txt文件_python解析文本文件

    python解析的文件原始数据: 汉皇重色思倾国,御宇多年求不得. 杨家有女初长成,养在深闺人未识. 天生丽质难自弃,一朝选在君王侧. 回眸一笑百媚生,六宫粉黛无颜色. 春寒赐浴华清池,温泉水滑洗凝脂 ...

最新文章

  1. 利用CSS定位背景图片
  2. 安卓samba软件_Android Samba Client
  3. qt中显示文件夹下的目录及文件的过滤
  4. Enhanced ABAP Development with Core Data Services (CDS)
  5. SQL SERVER 中如何用脚本管理作业
  6. java生产者消费者问题代码分析
  7. 深度学习(十五)基于级联卷积神经网络的人脸特征点定位
  8. OSChina 周二乱弹 —— 加班的代码不要枉费了我的童子功
  9. Class.forName(com.mysql.jdbc.driver);
  10. Java8 HashMap之tableSizeFor
  11. godaddy虚拟主机access数据库中文乱码的解决方法
  12. (day 34 - 哈希表 or 双指针 )剑指 Offer 57. 和为s的两个数字
  13. 11g Database Installation flow
  14. Web开发之Servlet
  15. 【数据结构 严蔚敏版】 链表基本操作
  16. 计算机论文要多少字,期刊论文一般多少字
  17. 司空见惯 - 洪荒之力
  18. 优酷视频如何登录优酷账号?
  19. Curator的基本使用
  20. 一文搞懂InheritedWidget局部刷新机制

热门文章

  1. IOS 关于NSString类型的属性为什么有时用copy,有时用strong呢?
  2. .net Windows服务程序和安装程序制作图解
  3. 微信助力活动表结构设计
  4. 20180316 数组查询时间复杂度
  5. BZOJ Usaco 1616 Cow Travelling
  6. Django 应用开发(2)
  7. 我的java开发规范
  8. LeetCode59 Spiral Matrix II
  9. FireDac 的数据库批量语句提交(高效)
  10. Activity之间的信息传递方式