Python中write和writelines有何本质区别?
- 内容介绍
- 文章标签
- 相关推荐
本文共计420个文字,预计阅读时间需要2分钟。
一、传入的参数类型要求不同:
1.file.write(str) 需要传入一个字符串作为参数,否则会报错。
二、示例代码:
pythonwrite(字符串 + [‘a’, ‘b’, ‘c’])with open('20200222.txt', 'w') as fo: fo.write(['a', 'b', 'c'])一、传入的参数类型要求不同:
1、 file.write(str)需要传入一个字符串做为参数,否则会报错。
write( "字符串")
with open('20200222.txt','w') as fo: fo.write([‘a','b','c']) #错误提示:TypeError: write() argument must be str, not list
2、 file.writelines(sequence)可以有两种:字符串和字符序列,传入字符序列时,如果需要换行,则每个序列元素末尾需要有“\n”换行符才能达到所要输出的格式要求。
本文共计420个文字,预计阅读时间需要2分钟。
一、传入的参数类型要求不同:
1.file.write(str) 需要传入一个字符串作为参数,否则会报错。
二、示例代码:
pythonwrite(字符串 + [‘a’, ‘b’, ‘c’])with open('20200222.txt', 'w') as fo: fo.write(['a', 'b', 'c'])一、传入的参数类型要求不同:
1、 file.write(str)需要传入一个字符串做为参数,否则会报错。
write( "字符串")
with open('20200222.txt','w') as fo: fo.write([‘a','b','c']) #错误提示:TypeError: write() argument must be str, not list
2、 file.writelines(sequence)可以有两种:字符串和字符序列,传入字符序列时,如果需要换行,则每个序列元素末尾需要有“\n”换行符才能达到所要输出的格式要求。

