[英]How do I make my code add new inputs on a new line in a document while using the .writelines in python?
[英]Why do i get an added 'i' character at the end of string while using .writelines() in Python ?
我使用该 python 代码获取 3 个输入并写入文件,但是当我查看 /tmp 下名为 output.txt 的文件时,我看到在字符串末尾添加了一个“i”。为了说明,当我作为输入给出时a,b 和 c,分别我得到这样一个输入:ab ci。我能做些什么来解决这个问题?
name = raw_input("Name:")
email = raw_input("Email:")
phone = raw_input("Telephone:")
a=open("/tmp/output.txt","w")
a.writelines( name+' '+email+' '+phone)
a.close()
我不:
$ cat >/tmp/t <<_EOF_
> name = raw_input("Name:")
> email = raw_input("Email:")
> phone = raw_input("Telephone:")
>
> a=open("/tmp/output.txt","w")
> a.writelines( name+' '+email+' '+phone)
> a.close()
> _EOF_
$ (echo "John Smith"; echo "nobody@example.com"; echo "123-45-678") | python /tmp/t
$ hexdump -C /tmp/output.txt
00000000 4a 6f 68 6e 20 53 6d 69 74 68 20 6e 6f 62 6f 64 |John Smith nobod|
00000010 79 40 65 78 61 6d 70 6c 65 2e 63 6f 6d 20 31 32 |y@example.com 12|
00000020 33 2d 34 35 2d 36 37 38 |3-45-678|
00000028
phone
的末尾有'i'
,或者稍后将'i'
附加到文件中。
.writelines()
接受一个字符串序列。 它在这里工作是偶然的(任何字符串也是一个字符串的序列)。 要将字符串写入文件,您可以使用.write()
代替。 如果需要,请在末尾添加'\\n'
; .writelines()
和.write
都不会为您添加换行符。
尝试打印
print phone
查看变量“phone”的内容是否符合您的预期
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.