简体   繁体   English

Python:如何将用户文本输入存储在文件中?

[英]Python: How to store user text input in a file?

I'm trying to prompt the user to enter a block of text until he/she types EOF on a separate line by itself. 我试图提示用户输入文本块,直到他/她自己在单独的行上键入EOF为止。 After that, the program should present him/her with a menu. 之后,程序应向他/她提供菜单。 When I go to Option 1, it only prints out EOF and not everything that was typed earlier. 当我转到选项1时,它仅打印出EOF,而不打印出先前键入的所有内容。 Why is this? 为什么是这样?

Let's say I type "Hi I like pie" as my block of text. 假设我输入“嗨,我喜欢馅饼”作为我的文本块。 I type EOF to head to the menu and type option 1. I expect "Hi I like pie" to pop up but only the letters EOF does. 我键入EOF以转到菜单并键入选项1。我希望弹出“嗨,我喜欢馅饼”,但只有字母EOF可以。 How do I fix this? 我该如何解决? How do I "feed" a Python file? 如何“喂” Python文件?

#Prompt the user to enter a block of text.
done = False
while(done == False):
    textInput = input()
    if textInput == "EOF":
        break

#Prompt the user to select an option from the Text Analyzer Menu.
print("Welcome to the Text Analyzer Menu! Select an option by typing a number"
    "\n1. shortest word"
    "\n2. longest word"
    "\n3. most common word"
    "\n4. left-column secret message!"
    "\n5. fifth-words secret message!"
    "\n6. word count"
    "\n7. quit")

option = 0

while option !=7:
    option = int(input())

    if option == 1:
        print(textInput)

The reason is that in your while loop, you loop until textInput is equal to EOF , so you only print EOF 原因是在while循环中,循环直到textInput等于EOF ,所以您只打印EOF

You can try something like this (by using a nextInput variable to "preview" the next input): 您可以尝试执行以下操作(通过使用nextInput变量“预览”下一个输入):

#Prompt the user to enter a block of text.
done = False
nextInput = ""
while(done == False):
    nextInput= input()
    if nextInput== "EOF":
        break
    else:
        textInput += nextInput

When you set 设定时

textInput = input()

you throw away the old input. 您会丢弃旧的输入。 If you want to keep all the input, you should make a list: 如果要保留所有输入,则应列出:

input_list = []
text_input = None
while text_input != "EOF":
    text_input = input()
    input_list.append(text_input)

Each time the user types a new line in, your textInput variable gets overwritten. 每次用户输入新行时,您的textInput变量都会被覆盖。

You could do 你可以做

textInput = ''
done = False
while(done == False):
    input = input()
    if input == "EOF":
        break
    textInput += input

Also, you don't need to use both a done variable and the break statement. 同样,您不需要同时使用done变量和break语句。 you could either do 你可以做

done = False
while(done == False):
    textInput += input()
    if textInput == "EOF":
        done = True

or 要么

while True:
    textInput += input()
    if textInput == "EOF":
        break

You need to save every line typed in your while loop as it is typed. 您需要保存键入时在while循环中键入的每一行。 Each time the user types a new line, the variable textInput is overwritten. 每次用户键入新行时,变量textInput将被覆盖。 You can use store text to a text file like this: 您可以使用将文本存储到文本文件中,如下所示:

writer = open("textfile.txt" , "w")
writer.write(textInput + "\n")

Insert this as an elif statement after your if in the while loop. 在while循环中的if之后,将其作为elif语句插入。 The "\\n" is a new line command which does not show up when the text is read, but tells the computer to start a new line. “ \\ n”是换行命令,在读取文本时不会显示,但会告诉计算机开始换行。

In order to read this file, use this code: 为了读取此文件,请使用以下代码:

reader = open("textfile.txt" , "r")
print(reader.readline()) #line 1
print(reader.readline()) #line 2

There are various other methods for reading the file the different ways you want to for you program, which you can research on your own. 还有多种其他方法可以使用您希望自己编程的方式来读取文件,您可以自行研究。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM