[英]sys.stdin.readlines() in pyinstaller
Here, I want to creat a simple exe program that removes duplicated people using pyinstaller. 在这里,我想创建一个简单的exe程序,该程序使用pyinstaller删除重复的人。
Since I want to get multiline of inputs, I used: 由于我想获得多行输入,因此我使用了:
text = sys.stdin.readlines()
but even though it works well on Jupyter notebook or spyder, after when I create and open its exe file, it does not show anything but just black window. 但是,即使它在Jupyter笔记本电脑或spyder上运行良好,当我创建并打开其exe文件后,它也只显示黑色窗口而没有显示任何内容。 if I remove that line, I know that it works, but only single line input will be allowed.
如果删除该行,我知道它可以工作,但是仅允许单行输入。
Also, I've try to switch these two, so input first then sys.stdin.readlines(): 另外,我尝试切换这两个,所以先输入,然后输入sys.stdin.readlines():
text = sys.stdin.readlines()
text = input('Type or Paste your text, and press Ctrl + Z.\n\n')
However, even though it allows multiline input, it only processes duplication function on the very first line of the input. 但是,即使它允许多行输入,它也只在输入的第一行处理复制功能。
Here is my full code. 这是我的完整代码。 Please help me out.
请帮帮我。 Thanks
谢谢
# -*- coding: utf-8 -*-
import re
import sys
def duplication():
text = sys.stdin.readlines()
text = input('Type or Paste your text, and press Ctrl + Z.\n\n')
text = re.split(", |-| |,", text)
text = list(filter(None, text)) # fastest
names = set()
remove = set()
for n in text:
if not n.startswith(("*", "-")):
n = n.lstrip()
if n in names:
remove.add(n)
elif n not in names:
names.add(n)
print(f'\n\nDuplicated person: {(sorted(remove))}\n\nTotal of {len(remove)}is/are removed, Total of {len(names)}is shown.')
print (sorted(names))
def next_move():
nxt = input("\n\nWhat do you want to do next?\n 1. Enter another text\n 2. exit\n\n")
if nxt == '1':
duplication()
next_move()
elif nxt == '2':
exit()
else:
print('Please choose between 1 and 2.')
next_move()
def overall():
duplication()
next_move()
overall()
Your input handling is a little out, and you also don't appear to realise that the readlines()
method returns a list of all the lines until the end of file. 您的输入处理有点麻烦,并且您似乎也没有意识到
readlines()
方法会返回所有行的列表,直到文件结尾。 Therefore the input
call won't see any input - which might make it hang. 因此,
input
呼叫将看不到任何输入-这可能使其挂起。
Since you don't discuss the data format I'm assuming that 由于您不讨论数据格式,所以我假设
'#'
and '-'
should be ignored, as should empty lines '#'
和'-'
开头的行应被忽略,空行也应被忽略 Your duplication
function should look more like this (untested): 您的
duplication
功能应该看起来像这样(未经测试):
def duplication():
print('Type or Paste your text, and press Ctrl + Z.\n\n')
text = sys.stdin.readlines()
names = set()
remove = set()
for n in text:
n = n.strip()
if n and n.startswith(("*", "-")):
continue # straight to next line
if n in names:
remove.add(n)
elif n not in names: # else: would be just as good!
names.add(n)
print(f'\n\nDuplicated person: {(sorted(remove))}\n\nTotal of {len(remove)}is/are removed, Total of {len(names)}is shown.')
print (sorted(names))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.