Python简单文本处理(一)
来源:互联网 发布:linux安装包命令 编辑:程序博客网 时间:2024/06/05 22:49
1、join和split
join用来连接字符串,将列表中的单词连接起来从而生成字符串。
>>> ' '.join(['Monty','Python'])'Monty Python'
>>> ','.join(['Hello','World'])'Hello,World'split则用于分割字符串
>>> 'Monty Python'.split()['Monty', 'Python']>>> 'Hello,World'.split(',')['Hello', 'World']2、endswith
>>> li = ['visited','me','looked']>>> for w in li:if w.endswith('ed'):print(w)visitedlooked3、html文本处理
>>> from urllib.request import urlopen>>> url = "http://www.baidu.com">>> html = urlopen(url).read()>>> html[:80]b'<!DOCTYPE html><!--STATUS OK--><html><head><meta http-equiv="content-type" conte'版本3.0之前则是
>>> from urllib import urlopen
4、python处理RSS(feedparser)
feedparser下载地址http://feedparser.org
5、使用python内置方法open()读取本地文件
>>> f = open('README.txt')>>> raw = f.read()>>> raw[:80]'This is Python version 3.4.1\n============================\n\nCopyright (c) 2001, 2'使用type()查看类型
>>> type(raw)<class 'str'>列出当前目录下的文件列表
>>> import os>>> os.listdir('.')上一目录
>>> os.listdir('..')6、读取文件内容
>>>f = open('README.txt')>>> for line in f:print(line.strip())7、从版本3.0开始去掉了raw_input(),改用input()
>>> input('Enter some text: ')Enter some text: abc'abc'8、find()函数
>>> s = 'Monty Python'>>> s.find('Python')69、列表是可变的,字符串是不可变的
>>> li = ['Paul','George','John']>>> li[0] = 'Lennon'>>> li['Lennon', 'George', 'John']
>>> s = 'George'>>> s[0] = 'D'Traceback (most recent call last): File "<pyshell#68>", line 1, in <module> s[0] = 'D'TypeError: 'str' object does not support item assignment
0 0
- Python简单文本处理(一)
- python文本处理常用工具代码(一)
- Python文本处理常用代码总结(一)
- Python文本处理(1)
- Python文本处理(2)
- python 文本处理(分割)
- python cookbook(一) 文本
- Python中的文本(一)
- python学习(1)--python处理文本
- python处理文本一处代码优化过程
- 简单的文本处理
- 简单的文本处理
- 简单的文本处理
- 简单的文本处理
- Linux简单文本处理
- 简单的文本处理
- Python(10):文本处理,正则表达式
- Python 中的文本处理
- POJ 2378 Tree Cutting 子树统计
- 傅里叶变换理解之三
- hdu 5062 Beautiful Palindrome Number(水题)
- C++中的单例模式
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--客户端对象模型和REST APIs概览 客户端对象模型(CSOM)基础
- Python简单文本处理(一)
- hdu 5063 Operation the Sequence(模拟)
- 如何使用GCD
- 走进大学殿堂 开启大学时光
- JavaScript学习笔记:prompt提问对话框
- BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第9章节--客户端对象模型和REST APIs概览 托管代码(.NET)
- GC Root
- ZipArchive压缩和解压缩文件
- poj 2482 Stars in Your Window(扫描线)