如何将输入保存到文本文件中?
How do you save the inputs into text file?
如何将程序的输入保存到程序的文件中。
仅 Python:
我需要将其添加到此:
再次完成任务 2
sentence = input("Please input a sentence: ")
print(sentence)
word=input("Please enter word and position wil be shown: ")
if word in sentence:
print("Found word")
else:
print ("Word not found")
但是我一点头绪都没有
我想这就是你要的
text_file = open("Output.txt", "w")
text_file.write(stuff)
text_file.close()
您的问题似乎有两个主要部分:如何在 Python 中获取输入以及如何将数据保存到 Python.
中的文件中
从终端获取输入:
>>> data = input('Input: ')
>>> Input: hello, world!
要保存到文件:
>>> with open('output.txt', 'w') as f:
>>> f.write(data)
您可以找到有关输入的更多信息here and file i/o here
编辑 0:@Gazzer,如果你想保存 sentence + input
你需要做 f.write(sentence + input)
而不是使用 .save()
.
编辑 1:@Gazzer,我得到了类似下面的东西(注意:代码不显示找到的单词的位置):
sentence = input("Please input a sentence: ")
word = input("Please enter word and position wil be shown: ")
with open('output.txt', 'w') as f:
f.write('{} {}'.format(sentence, word))
如果您 运行 遇到更多问题,网络上有数百种资源可以寻求帮助。堆栈溢出,learnprogramming,等等。
下次您提问时,如果您提供您正在处理的代码片段以及 problem/errors 是什么,这对那些回答者真的很有帮助。
如何将程序的输入保存到程序的文件中。 仅 Python: 我需要将其添加到此:
再次完成任务 2
sentence = input("Please input a sentence: ")
print(sentence)
word=input("Please enter word and position wil be shown: ")
if word in sentence:
print("Found word")
else:
print ("Word not found")
但是我一点头绪都没有
我想这就是你要的
text_file = open("Output.txt", "w")
text_file.write(stuff)
text_file.close()
您的问题似乎有两个主要部分:如何在 Python 中获取输入以及如何将数据保存到 Python.
中的文件中从终端获取输入:
>>> data = input('Input: ')
>>> Input: hello, world!
要保存到文件:
>>> with open('output.txt', 'w') as f:
>>> f.write(data)
您可以找到有关输入的更多信息here and file i/o here
编辑 0:@Gazzer,如果你想保存 sentence + input
你需要做 f.write(sentence + input)
而不是使用 .save()
.
编辑 1:@Gazzer,我得到了类似下面的东西(注意:代码不显示找到的单词的位置):
sentence = input("Please input a sentence: ")
word = input("Please enter word and position wil be shown: ")
with open('output.txt', 'w') as f:
f.write('{} {}'.format(sentence, word))
如果您 运行 遇到更多问题,网络上有数百种资源可以寻求帮助。堆栈溢出,learnprogramming,等等。
下次您提问时,如果您提供您正在处理的代码片段以及 problem/errors 是什么,这对那些回答者真的很有帮助。