python list转换字符串报错TypeError: sequence item 0: expected str instance, int found
2017-03-28 21:16
1291 查看
今天敲小例子,报了错TypeError: sequence item 0: expected str instance, int found
小例子:list1=[1,'two','three',4]
print(' '.join(list1))
以为会打印 1 two three 4
结果报了错
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
print(" ".join(list1))
TypeError: sequence item 0: expected str instance, int found
上网查了资料,说list包含数字,不能直接转化成字符串。
解决办法:print(" ".join('%s' %id for id in list1))
即遍历list的元素,把他转化成字符串。这样就能成功输出1 two three 4结果了。
小例子:list1=[1,'two','three',4]
print(' '.join(list1))
以为会打印 1 two three 4
结果报了错
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
print(" ".join(list1))
TypeError: sequence item 0: expected str instance, int found
上网查了资料,说list包含数字,不能直接转化成字符串。
解决办法:print(" ".join('%s' %id for id in list1))
即遍历list的元素,把他转化成字符串。这样就能成功输出1 two three 4结果了。
相关文章推荐
- TypeError: sequence item 0: expected str instance, int found的解决办法
- python 字符串(str)和列表(list)的互相转换
- python 中list(列表),tupe(元组),str(字符串),dict(字典)之间的相互转换
- Python字符串与数字拼接报错解决TypeError: cannot concatenate 'str' and 'int' objects
- Python字符串与数字拼接 TypeError: cannot concatenate 'str' and 'int' objects
- python 点滴记录4:TypeError: range() integer end argument expected, got str.
- python 字符串(str)和列表(list)的互相转换
- python TypeError: Expected int32, got list containing Tensors of type '_Message' instead.
- TypeError: cannot concatenate 'str' and 'list' objects和Python读取和保存图片
- python学习之路-字符串(str)和列表(list)的互相转换
- python:字符串和数字拼接 (TypeError: Can't convert 'int' object to str implicitly)
- Python List find方法报错 TypeError: 'str' does not support the buffer interface
- python "".join() 出错TypeError: sequence item 0: expected string
- python实现字符串类型 str 转换为 list 类型(unicode 转换为 list)
- Python+client_connection.sendall(http_resp)+TypeError: 'str' does not support the buffer interface
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python持久化时抛出异常:TypeError: must be str, not bytes
- Python技巧——list与字符串互相转换
- python中的列表(list),字典(dict),字符串(str)的基本操作
- Python使用eval强制转换字符串为字典时报错:File "<string>", line 1, in <module> NameError: name 'nan' is not defined