TypeError: write() argument must be str, not list211111111

28 篇文章 0 订阅
16 篇文章 0 订阅

TypeError: write() argument must be str, not list

TypeError: write() argument must be str, not list

这种问题其实很简单,就只是由于write里面必须是str类型,你强制转换成str就ok了

file.write(result)

变成这个:

file.write(str(result))

解决了!

总之

这种问题对于我们小白来说就是他需要什么类型我们就强转为该类型就好了

  • 22
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
This error occurs when you try to pass a generator object as an argument to the built-in `write()` function in Python. The `write()` function is used to write data to a file object or a stream, and it expects a string as its argument. Here's an example of how this error can occur: ``` # Example 1: Passing a generator to write() def my_generator(): yield 'Hello' yield 'world' with open('output.txt', 'w') as f: f.write(my_generator()) # ERROR: write() argument must be str, not generator ``` In this example, we define a generator function `my_generator()` that yields two strings: "Hello" and "world". We then try to write the output of this generator to a file using the `write()` function, which results in the error message "write() argument must be str, not generator". To fix this error, we need to convert the generator output to a string before passing it to `write()`. One way to do this is to use the `join()` method of the string object to concatenate the generator output into a single string: ``` # Example 2: Converting generator output to string before passing to write() def my_generator(): yield 'Hello' yield 'world' with open('output.txt', 'w') as f: f.write(''.join(my_generator())) # OK: generator output is concatenated into a single string ``` In this example, we use the `join()` method to concatenate the output of the `my_generator()` function into a single string, which we then pass to the `write()` function. This should write the string "Helloworld" to the file "output.txt".

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值