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

文件写入操作时,报错:TypeError: write() argument must be str, not list

原因:python写入的内容要是字符串类型的

上代码:

fp = open("a.txt","w")
fp.write([1,2,3])
fp.close()

>>> fp = open("a.txt","w")
>>> fp.write([1,2,3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not list
>>> fp.close()

写入内容为字符串类型则ok

fp = open("a.txt","w")
fp.write(‘[1,2,3]‘)#将文件内容处理为字符串类型
fp.close()

原文地址:https://www.cnblogs.com/rychh/p/9742203.html

时间: 2024-08-26 17:21:08

Python_异常:TypeError: write() argument must be str, not list的相关文章

Python错误TypeError: write() argument must be str, not bytes

2016-07-03 20:51:25 今天使用Python中的pickle存储的时候出现了以下错误: TypeError: write() argument must be str, not bytes 网上搜索才发现原来是文件打开的方式有问题. 之前文件打开的语句是: f=open("list.pkl","w+") 然后使用二进制方式打开就没有这个问题: f=open("list_account.pkl","wb+") 产

Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got &#39;str&#39;

python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io import BytesIO as StringIO   Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+

今天写上传文件代码,如下 def uploadHandle(request): pic1=request.FILES['pic1'] picName=os.path.join(settings.MEDIA_ROOT,pic1.name) with open(picName,'w') as pic: for c in pic1.chunks(): pic.write(c) return HttpResponse(picName) 出现TypeError: write() argument must

TypeError: write() argument must be str, not bytes报错

TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: 然后使用二进制方式打开就没有这个问题: with open('C:/result.pk','wb+') as fp: 产生问题的原因是因为存储方式默认是二进制方式. 原文地址:https://www.cnblogs.com/cheng10/p/10107838.html

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

w文件打开以 '二进制'  方式: with open('teacher.html','wb+') as f: f.write(response.body) 要写入"中文",防止乱码: fo = open("temp.txt", "wb+") str = '中文' str = str.encode('utf-8') fo.write(str) fo.close()

statsmodels.tsa.arima_model预测时报错TypeError: int() argument must be a string, a bytes-like object or a number, not &#39;Timestamp&#39;

在 python 中用 statsmodels创建 ARIMA 模型进行预测时间序列: import pandas as pd import statsmodels.api as sm df = pd.read_csv("data.csv", index_col=0, parse_dates=True) mod = sm.tsa.statespace.SARIMAX(df['price'], enforce_stationarity=False, enforce_invertibili

tensorflow中gradients的使用以及TypeError: Fetch argument None has invalid type &lt;class &#39;NoneType&#39;&gt;错误解析

在反向传播过程中,神经网络需要对每一个loss对应的学习参数求偏导,算出的这个值也就是梯度,用来乘以学习率更新学习参数使用的,它是通过tensorflow中gradients函数使用的. 我们根据官方文档对函数原型进行解析 官方文档中函数原型以及参数如下: tf.gradients( ys, xs, grad_ys=None, name='gradients', colocate_gradients_with_ops=False, gate_gradients=False, aggregatio

解决 TypeError: (&#39;Keyword argument not understood:&#39;, &#39;padding&#39;)

subsample 在使用 CNN的时候,报错: TypeError: ('Keyword argument not understood:', 'padding') 原因:padding 是Keras 2.X的语法,而我的PC安装的是 Keras 1.X版本. 二者的API 有一些地方是有变化的. 如下: 解决 TypeError: ('Keyword argument not understood:', 'padding') 原文地址:https://www.cnblogs.com/shen

python_异常报错

一.报错类型 AttributeError 试图访问一个对象没有的树形,比如foo.x,但是foo没有属性x IOError 输入/输出异常:基本上是无法打开文件 ImportError 无法引入模块或包:基本上是路径问题或名称错误 IndentationError 语法错误(的子类) :代码没有正确对齐 IndexError 下标索引超出序列边界,比如当x只有三个元素,却试图访问x[5] KeyError 试图访问字典里不存在的键 KeyboardInterrupt Ctrl+C被按下 Nam