在 Python 中调用 .x 文件 (Linux) 并列出

发布于 2024-12-19 17:26:58 字数 1075 浏览 4 评论 0原文

我有以下代码:

# Initialisations

filename='/home/Admin/Desktop/Ncapa/i_capa.txt'
f = open(filename, 'r') 

ty = "r"
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()

d = float(f.readline())
D = float(f.readline())
BB = float(f.readline())
vrR = float(f.readline())
P = float(f.readline())
f0 = float(f.readline())
f1 = float(f.readline())

f.close()
os.system('ls -lt > ./capacite.x')
filename2 = '/home/Admin/Desktop/Ncapa/o_capa.txt'
f = open(filename2, 'r')

f.readline()
f.readline()
f.readline()
f.readline()
c0 = float(f.readline())

f.close()   

我的第一个问题(正如您可能已经猜到的那样,是一个非常简单的问题),有没有办法将 f.readline 块简化为简单的东西?我总是对如何读取文本文件然后从列表中获取值分配给变量感到困惑。所以我最终这样做了,因为我喜欢它的工作方式,但不喜欢整个代码的外观。我没有使用 f.readlines() 因为我很困惑如何从使用它的列表中获取某些值。

我的第二个问题对应于以下行:

os.system('ls -lt > ./capacite.x')

有没有办法在 Linux 中的 Python 中执行 .x 文件?该行似乎不会引起任何问题,但我知道它也不起作用,因为该文件生成 o_capa.txt 文件,到目前为止,我必须手动填写该文件来测试程序的其余部分。

任何帮助将不胜感激=)

I have the following code:

# Initialisations

filename='/home/Admin/Desktop/Ncapa/i_capa.txt'
f = open(filename, 'r') 

ty = "r"
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()
f.readline()

d = float(f.readline())
D = float(f.readline())
BB = float(f.readline())
vrR = float(f.readline())
P = float(f.readline())
f0 = float(f.readline())
f1 = float(f.readline())

f.close()
os.system('ls -lt > ./capacite.x')
filename2 = '/home/Admin/Desktop/Ncapa/o_capa.txt'
f = open(filename2, 'r')

f.readline()
f.readline()
f.readline()
f.readline()
c0 = float(f.readline())

f.close()   

My first question (as you may have already guessed, is a very simple one), is there a way of reducing the f.readline block to something simple? I always get confused how to read a text file and then take values from a list to assign to variables. So I ended up doing it this way as I like the way it works but don't like the look of the code as a whole. I haven't used f.readlines() as I get confused how to get certain values from the list you get using it.

My second question corresponding to the line:

os.system('ls -lt > ./capacite.x')

Is there a way of executing an .x file in Python in Linux? This line doesn't appear to cause any problems but I know it doesn't work either as this file produces the o_capa.txt file which up until this point I have had to fill in by hand to test the rest of my programme.

Any help would be greatly appreciated =)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

讽刺将军 2024-12-26 17:26:59

您可以在类似于下面的循环中运行 readline

try:
    [fin.xreadlines().next() for i in xrange(0,<some_limit>)]
    d = float(fin.xreadlines().next())
except StopIteration:
    None

You can run the readline in a loop something similar to the below

try:
    [fin.xreadlines().next() for i in xrange(0,<some_limit>)]
    d = float(fin.xreadlines().next())
except StopIteration:
    None
南…巷孤猫 2024-12-26 17:26:58

如果您知道每个变量的名称和行,请创建一个字典:

position = {12: 'd',
            13: 'D', ... }
data = {}
with open(filename, 'r') as f:
    for i, line in enumerate(f):
        if i in position:
            data[position[i]] = float(line.strip())

然后像变量一样使用 data 字典,例如 data['d']

If you know the name and line for each variable, create a dictionary:

position = {12: 'd',
            13: 'D', ... }
data = {}
with open(filename, 'r') as f:
    for i, line in enumerate(f):
        if i in position:
            data[position[i]] = float(line.strip())

Then use the data dictionary as if it were variables, such as data['d'].

谈场末日恋爱 2024-12-26 17:26:58

你为什么不使用

f.readlines()

why don't you use

f.readlines()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文