python - 如何在另一个 python 文件中运行我的 python 文件?

标签 python python-2.7 pandas numpy python-requests

我有以下内容:

# script_1

from ephem import *
import time, math, commands, sys, os

elements = open('orbital_elements.txt')

# Then does some stuff and takes information from the text file, calculating diff_x, diff_y, diff_z

return diff_x, diff_y, diff_z

然后我有第二个脚本。此脚本将 script_1 作为模块导入,然后将在一个范围内循环,每次更改 orbital_elements.txt 并在新文本文件上运行 script_1 以每次返回 diff_x、diff_y、diff_z 的新值:

#script_2

from __future__ import division
import numpy as np
import pandas as pd
import os
import script_1

df_input = pd.read_csv("sampling.txt", sep = ",", index_col=False)

def convert(x):
    # Input single row of df_input

    epoch_osculation = '2018 02 22'
    M = x['M']
    AOP = x['AOP']
    LOAN = x['LOAN']
    INCL = x['INCL']
    e = x['e']
    a = x['a']

    text_file = open("orbital_elements.txt", "w")
    text_file.write("Object: 1 ceres\n")
    text_file.write("Epoch of osculation     =  " + str(epoch_osculation) + "\n")
    text_file.write("Mean anomaly            =  " + str(M) + "\n")
    text_file.write("Argument of perihelion  =   " + str(AOP) + "\n")
    text_file.write("Long. of ascending node =   " + str(LOAN) + "\n")
    text_file.write("Inclination             =   " + str(INCL) + "\n")
    text_file.write("Eccentricity            =  " + str(e) + "\n")
    text_file.write("Semimajor axis          =  " + str(a))
    text_file.close()

index = np.arange(len(df_input))
df_output = pd.DataFrame(columns=['x', 'y', 'z'], index = index)

for i in range(len(df_input)):
    # Creates text file for script_2.py
    convert(df_input.iloc[i]) 

    script_1

    # Record data in a table
    #df_output['x'][i] = script_1.diff_x
    #df_output['y'][i] = script_1.diff_y
    #df_output['z'][i] = script_1.diff_z

不幸的是,每次我运行第二个脚本时,它都会返回相同的值,就好像文本文件 orbital_elements.txt 没有被更改一样。

我知道文本文件每次都在更新,因为我已经测试过了。问题似乎在于 script_1 无法识别 orbital_elements.txt 的更新版本并且每次都返回相同的值。

我如何调整它以便第一个脚本在导入到 script_2 时考虑到 orbital_elements.txt 的更新版本?

最佳答案

script_1 的主体将在导入后立即运行:

from ephem import *
import time, math, commands, sys, os

elements = open('orbital_elements.txt')

# Then does some stuff and takes information from the text file, calculating diff_x, diff_y, diff_z

return diff_x, diff_y, diff_z

所以上面所有的代码都立即运行在:

from __future__ import division
import numpy as np
import pandas as pd
import os
import script_1   # <- This line

您需要将代码放入一个函数中,只在适当的时候调用它:

from ephem import *
import time, math, commands, sys, os

def main():
    elements = open('orbital_elements.txt')

    # Then does some stuff and takes information from the text file, calculating diff_x, diff_y, diff_z

    return diff_x, diff_y, diff_z

然后在需要时使用 script_1.main()

关于python - 如何在另一个 python 文件中运行我的 python 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48999187/

相关文章:

python-2.7 - 执行错误 : Can't open jupyter notebook on command line (MacOS)

python - 如何创建一个新列,显示前 5 行和剩余行作为杂项?

python - X Y Z 数组数据到热图

python - 转换字典键并搜索它们

python - Pygame 的当前 GUI 选项

python - 在 Pandas 中读取、选择和重新排列列

python-3.x - 导入 Pandas 为 pd ImportError : No module named pandas

python - 将 pandas 数据框转换为 python 字典

python - 我尝试在 VS code 中安装 cx_Oracle 并收到错误 Microsoft Visual C++ 14.0 或更高版本是必需的

python - 在 python 单元测试期间打开配置或文件的好方法是什么?