【语言处理与Python】3.9格式化:从链表到字符串
2013-05-24 20:15
387 查看
从链表到字符串
silly=['We', 'called', 'him', 'Tortoise', 'because', 'he', 'taught', 'us', '.'] ‘ ’.join(silly) 'We calledhim Tortoisebecausehetaught us.’#join()方法适合于一个字符串的链表
字符串与格式
#字符串格式化表达式 for word in fdist: print ‘%s->%d;’%(word,fdist[word]) #也可以使用模版 template = 'Lee wantsa %sright now' menu= ['sandwich', 'spam fritter', 'pancake'] for snack in menu: print template %snack
排列
#设置宽度 '%6s' %'dog' width=6 '%-*s' %(width, 'dog') width=max(len(w) for w in words)
将结果写入文件
output_file=open(‘output.txt’,w) words=set(nltk.corpus.genesis.words(‘english-kjv.txt’)) for word in sorted(words): output_file.write(word+’\n’)
文本换行
有的时候,需要采取自动换行from textwrap import fill format=’%s(%d)’ pieces=[format % (word,len(word)) for word in saying] output=’ ’.join(pieces) wrapped=fill(output) print wrapped
相关文章推荐
- Python语言总结 4.2. 和字符串(str,unicode等)处理有关的函数
- 【语言处理与Python】3.2字符串:最底层的文本处理
- 【语言处理与Python】1.2将文本当作词链表
- 【脚本语言系列】关于Python基础知识处理字符串,你需要知道的事
- 2018.4.8(Python)字符串处理函数
- 了解 Python 语言中的时间处理
- python+NLTK 自然语言学习处理七:N-gram标注
- 轻松python文本专题-单独处理字符串每个字符的方法汇总
- python的字符串处理
- 【语言处理与Python】4.2序列
- python3 split(分离器)处理字符串的函数用法
- Python内置的字符串处理函数详细整理(覆盖日常所用)
- python 字符串string 处理函数
- Python:字符串处理
- 20161216C语言基础17_字符串处理函数之fgets与gets
- python之字符串处理
- 20161216C语言基础18_字符串处理函数
- python 字符串处理函数
- 在Python中处理字符串之isdecimal()方法的使用
- python字符串中的中文处理