手册中关于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()函数的更多相关文章

  1. python中split函数的使用

    最近学习python,对split函数做了下总结,内容如下:

  2. Python中split()函数的用法及实际使用示例

    Python中split()函数,通常用于将字符串切片并转换为列表. 一.函数说明: split():语法:str.split(str="",num=string.count(st ...

  3. Python进阶---python strip() split()函数实战(转)

    先看一个例子: >>> ipaddr = 10.122.19.10 File "", line 1 ipaddr = 10.122.19.10 ^ SyntaxE ...

  4. Python之Split函数

    python中的split()函数用来拆分一个字符串,通过指定的分隔符对字符串进行切割,返回切割后的字符串列表list. split()函数用法: str.split(str=' ',num = st ...

  5. Python之split()函数

    在Python的高级特性里有切片(Slice)操作符,可以对字符串进行截取.Python还提供split()函数可以将一个字符串分裂成多个字符串组成的列表. split()的语法挺简单的: str.s ...

  6. python中split()函数讲解

    本文讲述的是string.split(s[, sep[, maxsplit]]),针对string类型的split()函数.它主要是切割字符串,结果返回由字符串元素组成的一个列表,具体怎么使用看下面的 ...

  7. Python - 用python实现split函数

    # pattern支持字符或者字符串 def my_split(string, pattern): ret = [] len_pattern = len(pattern) while True: in ...

  8. python split()函数

    Python split()函数 函数原型: split([char][, num])默认用空格分割,参数char为分割字符,num为分割次数,即分割成(num+1)个字符串 1.按某一个字符分割. ...

  9. python中join和split函数

    一个是分割,一个是连接. 惯例,先看内部帮助文档 Help on method_descriptor: join(...) S.join(iterable) -> string Return a ...

随机推荐

  1. 设置ubantu的软件源地址

    查看所用的源 $ sudo vim /etc/apt/sources.list 由于安装的Ubuntu Server 16.04.1 LTS是英文版的,软件源就默认都是 us.archive.ubun ...

  2. ubantu 虚拟机无法查看windows共享目录

    初学linux,安装好虚拟机,安装好ubantu系统,启动系统后无法查看windows共享目录. 原因是没有安装 vmware tools 教程地址:http://www.linuxidc.com/L ...

  3. PHP 页面编码声明方法详解(header或meta)(转)

    编码格式有两种,一种是php文件本身的编码格式,如 editplus之类的编辑器在保存文件的时候允许你指定文件编码格式:另一种是php输出的文本的编码格式,这个信息只对浏览器生效,方法为<?ph ...

  4. Linux 输入子系统 input

    一.输入子系统 针对输入设备设计:触摸屏.键盘.按键.传感器.鼠标...... 二.每种设备都属于字符设备驱动,程序的写法步骤也相同 1.实现入口函数 xxx_init() 和卸载函数 xxx_exi ...

  5. Mvn+Jetty启动项目

    这里要注意,Mvn加jetty启动项目,主要用到的是Maven的jetty插件,和你下的Jetty服务器没什么关系. 我的运行环境是jdk1.7,Eclipse-mars,Maven是Eclipse自 ...

  6. HAProxy安装配置用于TCP的负载均衡

    HaProxy介绍 Haproxy是一个开源的高性能的反向代理或者说是负载均衡服务软件之一,它支持双机热备.虚拟主机.基于TCP和HTTP应用代理等功能.其配置简单,而且拥有很好的对服务器节点的健康检 ...

  7. ORACLE 存储过程解及表解锁和停止执行

    查看进程: select * from v$process 根据存储过程名称查找是否被锁: select * FROM dba_ddl_locks where name =upper('sp_1'); ...

  8. pycharm的自定义python模板

    #!/usr/bin/env python # encoding: utf-8 #set( $SITE = "http://blog.csdn.net/morgana" ) &qu ...

  9. HttpURLConnection连接网页和获取数据的使用实例

    HttpURLConnection是java.net 里面自带的一个类,非常好用.虽然现在很多人用阿帕奇的HttpClient,但HttpURLConnection也是个不错的选择. 其实使用方法非常 ...

  10. JSP页面生成验证码功能

    <%@ page language="java" contentType="text/html; charset=UTF-8" import=" ...