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 ...
随机推荐
- js生成1~100随机数
(function (min,max){ var range = max - min; var rand = Math.random(); var num = min + Math.round(ran ...
- Java 编码规范
package(包) 包名的命名规范:1.小写 2.至少有一层目录 3.域名倒置书写 package baidu; package com.baidu.www; Class(类)-----大驼峰法 类 ...
- Linux 应用层open调用驱动层open过程
内核版本:3.0.8 open.close.read.write.ioctl等等都是类似. ====================================================== ...
- 微信小程序,请求php后台返回json数据多出隐藏字符问题
这几天在做一个微信小程序注册登录页面的时候碰到一个问题,就是使用wx.request api的时候success中返回的JSON数据前面会多出空白字符,后面网上查了一下是说php bom头问题(详细介 ...
- 平台调用之如何利用VS2013 C#调试C++DLL库
对于托管代码调用非托管DLL文件,已经是非常普遍的事情,下面写一下如何通过托管代码(C#)像调试托管代码一样调试DLL中的代码. 注意:(1)[dll工程和调用dll的exe工程需要在同一个解决方案中 ...
- 杂项-公司:星巴克百科-un
ylbtech-杂项-公司:星巴克百科 星巴克(Starbucks)是美国一家连锁咖啡公司的名称,1971年成立,是全球最大的咖啡连锁店,其总部坐落美国华盛顿州西雅图市.星巴克旗下零售产品包括30多款 ...
- loadrunner 学习 1 —— 关于loadrunner的安装/破解
从网上下载了loadrunner 11, .iso格式的镜像文件,百度一下,发现要用专门的软件才能在windows7 下安装 iso,我选的是 软件魔方. 安装完破解时,略有曲折, 主要是要以管理员的 ...
- 配置ElasticSearch快捷启动
在/etc/init.d目录下新建文件elasticsearch #!/bin/sh #chkconfig: 2345 80 05 #description: es #export JAVA_HOME ...
- python开发面向对象基础:人狗大战学面向对象
一,通过函数写人狗大战这个故事 #!/usr/bin/env python #_*_coding:utf-8_*_ #1.函数 人模子 def person(name,level,life_value ...
- Oray.com花生壳路由器配置注意
当路由器不链接wan口,只链接lan口时,此路由器其实就是当做一个无线交换机使用了,在此种情况下,花生壳登录会失败,因为花生壳本身也认为这设备不是路由器.