【问题标题】:Use variables in a textfile as input [duplicate]使用文本文件中的变量作为输入[重复]
【发布时间】:2016-03-16 16:47:17
【问题描述】:

正如题目所说..

我想使用该文件

indata.txt:

f=lambda x,y:-2*x*y
xa=0
xb=2
A=2 
n=18

进入我的大计划:

from math import *

'''Function which calculates the d.e. using runge-kuttas formula'''
def runge(f,xa,xb,A,n):
    .....



indata=open("indata.txt","r")
runge(f,xa,xb,A,n)
indata.close()

所以我的问题是: 如何使用文本文件 indata.txt 中的变量作为更大程序的输入?

【问题讨论】:

  • 看起来像 python,为什么不直接调用它indata.py 并导入它?
  • 这是python,抱歉没有提及。该项目要求它是一个文本文件
  • 您仍然可以通过文件名导入它,请参阅我链接的欺骗。

标签: python input text-files var


【解决方案1】:

据我所知,您可以使用以下两个选项之一:
1. 将您的文件重命名为inpdata.py,如 cmets 和您的主文件使用中所述

from inpdata import *
  1. 使用python内置execfile()function。对于 Python3 execfile() 替代外观 here

【讨论】:

猜你喜欢
  • 2019-06-23
  • 2012-11-10
  • 2018-09-01
  • 2018-08-18
  • 1970-01-01
  • 2020-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多