Python文本替换(剔除、截取、添加字符串)


1. 删除字符串中的数字
要将以下文件中的数字和描述删除,只留下单词,并且转存为Objective-C的数组格式,第一步,先将数字全部剔除:
这里写图片描述

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import re

file = open("words.txt")
output = open("out.txt", 'wb')

for line in file.readlines():
    #删除字母、‘,’、‘()’、‘tab’以外的字符,即数字
    newline = filter(lambda ch: ch in ' abcdefghijklmnopqrstuvwxyz,() ', line)
    print newline
    output.write('@"' + newline + '\n')
file.close()
output.close()

处理完之后,得到一下文本文件:
这里写图片描述
2. 剔除单词后边所有字符串

#!/usr/bin/python
# -*- coding: UTF-8 -*-

file = open("out.txt", 'r')
output = open("dict.txt", "wb")

for line in file.readlines():
    #查找单词后头的tab,记录偏移量
    pos = line.find("   ", 0)
    print line[0:pos] + '", '
    #将tab之前的字符串,加上引号、逗号和换行符,写入数出文件
    output.write(line[0:pos] + '", \n')
file.close()
out.close()

得到最终我们需要的Objective-C单词数组的文本形式:
这里写图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章