Python的split()函数
手册中关于split()用法如下:
str.split(sep=None, maxsplit=-1)
Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).
If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].
For example:
>>> '1,2,3'.split(',')
['1', '2', '3']
>>> '1,2,3'.split(',', maxsplit=1)
['1', '2,3']
>>> '1,2,,3,'.split(',')
['1', '2', '', '3', '']
If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].
For example:
>>> '1 2 3'.split()
['1', '2', '3']
>>> '1 2 3'.split(maxsplit=1)
['1', '2 3']
>>> ' 1 2 3 '.split()
['1', '2', '3']
使用中碰到对一个数据集的处理碰到一点问题,最终用split()解决:
文件数据集:
0.0888 201 36.02 28 0.5885
0.1399 198 39.32 30 0.8291
...
目的将数据集保存到列表,以作接下来处理。
import os data = [] for lines in open(r"date.txt",'r').readlines():
lines.strip()
s = [x for x in lines.strip()]
data.append(s) print(data)
发现输出将每个字符都打印出来了,即0.0888为6个字符而不是期望中的1个,打印data[0]长度可知确实如此。
[['0', '.', '0', '8', '8', '8', ' ', ' ', ' ', ' ', ' ', '2', '0', '1', ' ', ' ', ' ', ' ', ' ', '3', '6', '.', '0', '2', ' ', ' ', ' ', ' ', ' ', '2', '8', ' ', ' ', ' ', ' ', ' ', '0', '.', '5', '8', '8', '5'], ['0', '.', '1', '3', '9', '9', ' ', ' ', ' ', ' ', ' ', '1', '9', '8', ' ', ' ', ' ', ' ', ' ', '3', '9', '.', '3', '2', ' ', ' ', ' ', ' ', ' ', '3', '0', ' ', ' ', ' ', ' ', ' ', '0', '.', '8', '2', '9', '1']]
利用split()函数按' '把每个数字分割出来:
for lines in open(r"date.dat",'r').readlines():
lines.strip()
s = [x for x in lines.strip().split()]
data.append(s) print(data)
print(len(data[0]))
输出:
[['0.0888', '', '36.02', '', '0.5885'], ['0.1399', '', '39.32', '', '0.8291']]
5
Python的split()函数的更多相关文章
- python中split函数的使用
最近学习python,对split函数做了下总结,内容如下:
- Python中split()函数的用法及实际使用示例
Python中split()函数,通常用于将字符串切片并转换为列表. 一.函数说明: split():语法:str.split(str="",num=string.count(st ...
- Python进阶---python strip() split()函数实战(转)
先看一个例子: >>> ipaddr = 10.122.19.10 File "", line 1 ipaddr = 10.122.19.10 ^ SyntaxE ...
- Python之Split函数
python中的split()函数用来拆分一个字符串,通过指定的分隔符对字符串进行切割,返回切割后的字符串列表list. split()函数用法: str.split(str=' ',num = st ...
- Python之split()函数
在Python的高级特性里有切片(Slice)操作符,可以对字符串进行截取.Python还提供split()函数可以将一个字符串分裂成多个字符串组成的列表. split()的语法挺简单的: str.s ...
- python中split()函数讲解
本文讲述的是string.split(s[, sep[, maxsplit]]),针对string类型的split()函数.它主要是切割字符串,结果返回由字符串元素组成的一个列表,具体怎么使用看下面的 ...
- Python - 用python实现split函数
# pattern支持字符或者字符串 def my_split(string, pattern): ret = [] len_pattern = len(pattern) while True: in ...
- python split()函数
Python split()函数 函数原型: split([char][, num])默认用空格分割,参数char为分割字符,num为分割次数,即分割成(num+1)个字符串 1.按某一个字符分割. ...
- python中join和split函数
一个是分割,一个是连接. 惯例,先看内部帮助文档 Help on method_descriptor: join(...) S.join(iterable) -> string Return a ...
随机推荐
- 基于jwt和角色的访问控制解决方案
0,主要解决两个问题:1身份验证(防止httpclient拼接请求),2权限控制 1,身份验证使用jwt,在java就是jjwt jwt可以比较好的整合restful,对无状态客户端比较友好,(用se ...
- CentOS上面搭建SVN服务器
1.安装svn sudo yum install subversion 查看安装位置 which svnserve 确认安装成功 svnserve --version 2.修改全局配置文件修改全局配置 ...
- Android Theme的使用
原文地址 http://www.cnblogs.com/Dentist/p/4369816.html Theme是一套UI控件和Activity的样式.可以给Application 和 activit ...
- onItemLongClick事件的监听
首先需要implements public class MainActivity extends AppCompatActivity implements OnItemLongClickListene ...
- android之Notification通知
我们在用手机的时候,如果来了短信,而我们没有点击查看的话,是不是在手机的最上边的状态栏里有一个短信的小图标提示啊?你是不是也想实现这种功能呢?今天的Notification就是解决这个问题的. pac ...
- CAN总线远程帧和错误帧
远程帧 通常,数据传输是由数据源节点(例如,传感器发出数据帧)自主完成的.但也可能存在目标节点向源节点请求发送数据的情况.要做到这一点,目标节点需发送一个远程帧,其中的标识符应与所需数据帧的标识符相匹 ...
- PHP $_SERVER变量
<?php #测试网址: http://localhost/t/test.php?id=5 //获取域名或主机地址 echo $_SERVER['HTTP_HOST']."<br ...
- dB2 索引相关
ALTER TABLE "XXXX"."tableA" PCTFREE 20 ; CREATE INDEX "schema"."X ...
- flask系列五之flask_script
1.一个简单的例子 在工程里面新建一个Python文件命名为manage.py (1)安装flask_script包,然后引入 from flask_script import Manager fro ...
- Java两种延时——thread和timer
在Java中有时候需要使程序暂停一点时间,称为延时.普通延时用Thread.sleep(int)方法,这很简单.它将当前线程挂起指定的毫秒数.如 try { Thread.currentThread( ...