简体   繁体   English

如何使用python将多个输入写入文本文件?

[英]how to write multiple inputs to text file using python?

I'm writing a ham radio logger. 我正在写一个业余无线电记录仪。 I want to write multiple inputs but with line breaks. 我想写多个输入但要换行。 Here's my example. 这是我的例子。

raw_input = question1(' text here ')
raw_input = question2(' ')
f.write(question1)
print ' '
f.write(question2)
print ' '

Is there a way to combine to 2 with a line break between? 有没有一种方法可以将两者之间的换行符合并为2? I've tried 我试过了

f.write(question1, "\n" question2, "\n")

but I get errors. 但我得到了错误。 I just want to make it shorter. 我只是想使其更短。 I'm very new to Python. 我是Python的新手。

q1=raw_input('question 1:') q2=raw_input('question 2:') with open('a.txt','w+') as f: f.writelines(q1+'\n') f.writelines('\n') f.writelines(q2+'\n')

input 输入
question1: hello? 问题1:你好?
question2:yes! 问题2:是的!

output: 输出:
hello? 你好?
"line break" “越线”
yes! 是!

By the way, if you want to output file with some special format, you could use json(import json). 顺便说一句,如果要输出某种特殊格式的文件,可以使用json(import json)。 AND, BEST to use " with " to handle file! AND,最好使用“ ”来处理文件!

you could also append a newline char \\n at the end of each input or store the inputs to a list and write it to a file 您还可以在每个输入的末尾添加换行符\\n或将输入存储到列表中并将其写入文件

itemlist = []
itemlist.append(raw_input("Your question"))
with open("text_file","w+") as outfile:
    outfile.write("\n".join(itemlist))

note that the with automatically closes the open file. 请注意, with自动关闭打开的文件。

The print is writing to standard output, not to f (which I assume is an open file handle). print正在写到标准输出,而不是f (我认为是打开文件句柄)。 The write method does not add newlines by itself, although you could of course add one. 尽管您当然可以添加一个换行符,但是write方法不会单独添加换行符。

f.write(''.join(question1, '\n', question2, '\n'))

Joining everything into one string just for printing isn't really necessary. 将所有内容仅连接到一个字符串以进行打印并不是真正必要的。 You can write things piecemeal, and/or use + for concatenation of strings, too: 你可以写零碎的东西,和/或使用+字符串的串联,太:

f.write(question1 + '\n')
f.write(question2 + '\n')

or even just 甚至只是

f.write(question1)
f.write('\n')
f.write(question2)
f.write('\n')

You could Python's string formatting to interpolate variables with formatting into strings, too: 您也可以使用Python的字符串格式将变量格式化为字符串,从而进行插值:

f.write('{0}\n{1}\n'.format(question1, question2))

or the once prevalent % interpolation operator: 或曾经流行的%插值运算符:

f.write('%s\n%s\n' % (question1, question2))

With Python 2.6+ you can from __future__ import print_function and then the Python 3 print function with an optional file= argument can be used. 在Python 2.6+中,您可以from __future__ import print_function ,然后可以使用带有可选file=参数的Python 3 print功能。 This is actually what I would recommend here. 这实际上是我在这里建议的。 print adds a newline by default (though it can be turned off if you wish). 默认情况下, print添加一个换行符(尽管您可以根据需要将其关闭)。

print(question1, file=f)
print(question2, file=f)

You were on the right path, just needed to make a few changes to your code. 您走在正确的道路上,只需要对代码进行一些更改。 Try this on for size. 试试看它的大小。

73s 73年代

question1 = raw_input('Any question? ')
question2 = raw_input('What is another question you have? ')

f = open('filename.txt', 'wb')
f.write("QUESTION 1 = " + str(question1) + "\nQUESTION 2 = " + str(question2))
f.close()

Output will look like this... 输出看起来像这样...

QUESTION 1 = What is ham radio? 问题1 =什么是火腿收音机?
QUESTION 2 = Do I need a license 问题2 =我需要许可证吗

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

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