问题描述:
在用pandas读取后缀名为.txt的文件,得到的数据格式为:'Series' object。需要读取特定行的特定的数字,本打算用正则表达式的模块“import re”来处理,但是发现把object格式的数据转换成string后才能使用,提示TypeError: expected string or bytes-like object。
解决办法:
发现pandas有str[1]可以免去数据格式转换,直接使用正则表达式[2]。具体使用的函数为pandas.Series.str.extract[3]或pandas.Series.str.extractall[4]。使用简单方便,所以特此纪录。
![788e78cb2a91f399763923ced3d16384.png](https://i-blog.csdnimg.cn/blog_migrate/778bd35635c8b094a43ff9cb7f776a9f.png)
我使用的具体过程:
![96b88893650d79fee004946346fc8ed7.png](https://i-blog.csdnimg.cn/blog_migrate/2ef3086892a3f34e589ab2ed55fba8b4.jpeg)
代码:
Real_time
参考
- ^https://pandas.pydata.org/pandas-docs/version/0.25/reference/series.html
- ^https://mp.weixin.qq.com/s/TdoZpteyM62NqtjRSTyt7g
- ^https://pandas.pydata.org/pandas-docs/version/0.25/reference/api/pandas.Series.str.extract.html#pandas.Series.str.extract
- ^https://pandas.pydata.org/pandas-docs/version/0.25/reference/api/pandas.Series.str.extractall.html#pandas.Series.str.extractall