python list转换字符串报错TypeError: sequence item 0: expected str instance, int found
2020-01-13 07:05
627 查看
python list转换字符串报错TypeError: sequence item 0: expected str instance, int found
场景:将列表转化为指定分隔符的字符串,列表当中有数字时会报如上错误。
user.txt:
alex:202cb962ac59075b964b07152d234b70:1234:0
tom:202cb962ac59075b964b07152d234b70:2234:0
test01:202cb962ac59075b964b07152d234b70::0
d_user内容如下: {'alex': ['alex', '202cb962ac59075b964b07152d234b70', '1234', '0'], 'tom': ['tom', '202cb962ac59075b964b07152d234b70', '2234', '0'], 'test01': ['test01', '202cb962ac59075b964b07152d234b70', '', '0']}
>>> l1 = ['alex', '202cb962ac59075b964b07152d234b70', '1234', 0]
'0'
>>> ':'.join(l1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sequence item 3: expected str instance, int found
解决办法:
>>> ':'.join('%s' %i for i in l1)
'alex:202cb962ac59075b964b07152d234b70:1234:0'
def dolock(username): file_content = "" d_user[username][3] = 1 for k in d_user: file_content += ':'.join(d_user[k]) + '\n' with open(user_file,'r+') as ufile: ufile.write(file_content)
def dolock(username): file_content = "" d_user[username][3] = 1 for k in d_user: file_content += ':'.join('%s' %i for i in d_user[k]) + '\n' with open(user_file,'r+') as ufile: ufile.write(file_content)
转载于:https://www.cnblogs.com/pyops/articles/9752075.html
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- python list转换字符串报错TypeError: sequence item 0: expected str instance, int found
- python list转换字符串报错TypeError: sequence item 0: expected str instance, int found
- TypeError: sequence item 0: expected str instance, int found
- TypeError: sequence item 0: expected str instance, int found的解决办法
- TypeError: sequence item 0: expected str instance, int found
- TypeError: sequence item 0: expected str instance, dict found 类型错误
- Python List find方法报错 TypeError: 'str' does not support the buffer interface
- python TypeError: Expected int32, got list containing Tensors of type '_Message' instead.
- python3 字符串/列表/元组(str/list/tuple)相互转换方法及join()函数的使用
- python 字符串(str)和列表(list)的互相转换
- Python字符串与数字拼接报错解决TypeError: cannot concatenate 'str' and 'int' objects
- Python 实现字符串转换成列表 实现str转换list
- python 字符串(str)和列表(list)的互相转换
- python 点滴记录4:TypeError: range() integer end argument expected, got str.
- python 中list(列表),tupe(元组),str(字符串),dict(字典)之间的相互转换
- TypeError: cannot concatenate 'str' and 'list' objects和Python读取和保存图片
- Python字符串与数字拼接 TypeError: cannot concatenate 'str' and 'int' objects
- python实现字符串类型 str 转换为 list 类型(unicode 转换为 list)
- python学习之路-字符串(str)和列表(list)的互相转换
- python:字符串和数字拼接 (TypeError: Can't convert 'int' object to str implicitly)