手册中关于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

最新文章

  1. FilterDispatcher已被标注为过时解决办法 &amp;gt;&amp;gt;&amp;gt; FilterDispatcher &amp;lt;&amp;lt;&amp;lt; is deprecated!
  2. Linux配置环境报“/usr/local/develop-tools/apache-maven-3.3.9/bin: 是一个目录“的解决方案
  3. CentOS7 服务器 JDK+TOMCAT+MYSQL+redis 安装日志
  4. Winform防止程序重复运行
  5. js总结-面向对象编程,DOM,BOM
  6. Android Studio如何发布APK
  7. 【英语】Bingo口语笔记(20) - i长短音
  8. Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
  9. mybatis关联查询
  10. KTV点歌系统播放原理
  11. php中yaf框架的服务器配置
  12. ProtocolBuffer for Objective-C 运行环境配置及使用
  13. C++解析六-继承
  14. linux 编译链接问题
  15. PAT 甲级 1096 Consecutive Factors
  16. OnCreateContextMenuListener接口简介
  17. 20155330 实验二 Java面向对象程序设计
  18. Mahout构建图书推荐系统【一起学Mahout】
  19. [Compose] 15. Applicative Functors for multiple arguments
  20. CMake入门实践

热门文章

  1. jQuery的ajax跨域实现
  2. Hadoop体系结构之 Yarn
  3. idea操作
  4. eclipse “”base revision” vs. “latest from repository”
  5. Eclipse里git提交冲突rejected – non-fast-forward
  6. C语言中的printf函数的输入输出问题
  7. Yii框架的一些系统函数
  8. file_put_contents(): supplied resource is not a valid stream resource
  9. tomcat 并发配置优化
  10. Java-API-Package:java.net百科