Python||报错:TypeError: can only join an iterable
一、问题描述
筛选all_seg.txt中的词语,去停用词,相关代码如下:
pkutest = [line.strip() for line in open ('all_seg.txt','r',encoding = 'utf-8').readlines()]
#GBK编码:是指中国的中文字符,其中它包含了简体中文与繁体中文字符,另外还有一种字符“gb2312”,这种字符仅能存储简体中文字符。#UTF-8编码:它是一种全国家通过的一种编码,如果你的网站涉及到多个国家的语言,那么建议你选择UTF-8编码,适用于国际化。final=[]
for n in pkutest:res=[]for n2 in n.split(" "):if n2 not in stopwords:res.append(n2)final.append(res)
查看变量final里的内容,情况如下:
使用DataFrame来表示表格式的数据,看起来会更直观一些,再查看一下final:
将变量final里写入到tingci.txt文件中,结果却报错:TypeError: can only join an iterable
二、报错分析与解决
报错原因是join括号里需要一个可迭代的值,可是final是一个字符串列表,怎么就不行了呢?
真的是这样吗???使用final.dtypes查看一下
打脸来的太快就像龙卷风~~~既然前面使用dataframe将final表格化过,那在进行for循环迭代之前将变量final强行转换为str类型就好啦~~~ 你以为问题就这样结束了吗?(我以为结束了,但是... ...
当我运行下一步代码想要统计单词出现的频率次数并写入excel表格时,又报错了(你以为的你以为... ...
报错内容TypeError: 'int' object is not iterable
当你改完一个bug却又新增一个bug时,这能说明什么呢?说明问题并没有解决(这不是废话嘛)至少不是从问题源头上解决T_T
报错的内容很相似,好像都是输入类型错误导致不能迭代。这好像在暗示什么,思来想去,好像dataframe那一步代码有问题,因为final.dtypes查看结果显示都是object类型。
要不试试把代码语句final=pd.DataFrame(final)注释掉?因为这句的作用只是查看内容而言,对处理数据关系不大
再次运行之前的代码,运行成功~~~
之所以写这么无聊的反复改bug过程,只要是想告诉自己:bug越改越多,可能是因为没有从源头上解决问题,或许是一行微不足道不起眼的代码,但它的存在会影响后面一连串程序的运行,这也许就是蝴蝶效应?!
OK,至此,本帖子完
Python||报错:TypeError: can only join an iterable相关推荐
- Python报错TypeError: ‘User‘ object is not iterable
Python报错TypeError: 'User' object is not iterable 原因 User对象是不可迭代的,我这里是因为User并不是list,所以是不可迭代的,所以不使用迭代即 ...
- Python报错TypeError: 'str' object is not callable
原文:http://blog.sina.com.cn/s/blog_71f3890901017rsh.html Python报错TypeError: 'str' object is not calla ...
- python报错 TypeError: an integer is required
问题描述: 在本地使用socket向NetAssist传送数据的时候,执行python文件后发现报出python 报错TypeError: an integer is required错误 代码: 1 ...
- Python报错 TypeError: Descriptors cannot not be created directly
Python报错TypeError: Descriptors cannot not be created directly 具体错误: TypeError: Descriptors cannot no ...
- Python报错TypeError: Descriptors cannot not be created directly
Python报错TypeError: Descriptors cannot not be created directly 具体错误: TypeError: Descriptors cannot no ...
- python报错TypeError: must be str, not int
python报错TypeError: must be str, not int 字符串拼接一个整型变量报错,代码如下 for i in range(1,586):res = 'test' + i 报错 ...
- Python 报错TypeError: expected string or bytes-like object
问题描述:Python 中报错TypeError: expected string or bytes-like object 解决方法:根据报错上方的提示信息,大体猜测到可能出错的具体地方.出现该报错 ...
- python报错:TypeError: 'str' object is not callable
这个问题遇到好多次了,这次记录一下, 问题重现 代码仅保留最重要的: # summaries 是一个集合 # 求和并打印 result = 0 for str in summaries: result ...
- 【20211123】【Python】修改list/array中的某些数值时,报错TypeError: can only assign an iterable,原因分析及正确的操作方式
一.提个需求先~ 有一个列表 a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],现在想要修改其中的 ...
- python报错TypeError: unsupported operand type(s) for -: ‘decimal.Decimal‘ and ‘float‘的解决方法
问题描述 在编写python代码时,进行小数位相减时出现 TypeError: unsupported operand type(s) for -: 'decimal.Decimal' and 'fl ...
最新文章
- 函数指针amp;绑定: boost::functoin/std::function/bind
- onkeyup,onkeydown和onkeypress的区别介绍
- java生成excel文件
- Python __slots__ 作用
- html省市区选择器代码,js实现一个省市区三级联动选择框代码分享
- C# 发送email邮件!
- 23 MM配置-采购-采购信息记录-定义屏幕格式
- 应用HttpWebRequest接收post或get数据
- CMU 15-213 Introduction to Computer Systems学习笔记(7) Machine-Level Programming-Data
- IDM无法连接到目标服务器
- 微信,你的野心到底有多大?
- 怎么查询电脑是否支持IPV6地址?
- react(let命令)
- 电脑装机兼容性测试软件,四款产品内部兼容性测试
- word题注的大写变成小写
- 有哪些提供比较好的网页模板网站?
- 24-哈希碰撞攻击是什么?
- hexo设置网站的图标Favicon
- FD 之 DingDing直播回放
- 汽车倒车雷达系统的设计与实现(一)
热门文章
- 使用Tensorflow训练LSTM+Attention中文标题党分类
- springboot yml语法 ${} 用法
- html5制作课程表app,课程表软件哪个好用些?5款便捷实用的课程表软件推荐
- 谈谈我当韭菜的投机经历
- arm+linux+usb驱动开发,Linux+ARM下的USB驱动开发
- 复数与三角函数的转换
- 美团Java后台开发面经
- /etc/sysconfig/network 文件
- 关于Eureka注册中心启动报错的原因
- STM32F429 21. LTDC 液晶屏幕