gpt4 book ai didi

python - 程序运行后如何保持在python交互式解释器中?

转载 作者:行者123 更新时间:2023-11-30 22:54:58 33 4
gpt4 key购买 nike

我正在从终端运行 python 程序

python -i abc.py <test.txt

但程序完成后,它不会保留在 python 中。

我想要什么:

Output:
4 21
>>>

发生了什么——

Output:
4 21
>>> >>>
Downloads:~$

通常如果我们发出命令python -i abc.py 运行程序后进入python交互模式。

程序(abc.py);

line=[]
f=int(raw_input())
i=0
while(i<f):
m=raw_input()
line.append(m)
i+=1

for i in line:
ind=i.find('$')
temp=''
for j in i[ind+1:]:
t=ord(j)
if((t>=48)&(t<=57)):temp=temp+j
elif(t!=32): break

temp='$'+str(int(temp))
print(temp)

测试.txt

1
I want to buy Car for $10000

谢谢

最佳答案

您重定向标准输入,因此当文件结束时,进程将退出。

首先从文件中读取内容,然后从标准输入中读取:

(cat test.txt && cat) | python -i abc.py

没有任何参数,cat 从标准输入读取。因此,在这种情况下,python 进程首先接收 cat test.txt 的输出,然后接收 cat 的输出,即 stdin。

请注意,这与使用不带重定向的 python -i abc.py 的行为并不完全相同。

关于python - 程序运行后如何保持在python交互式解释器中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37588467/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com