#!/usr/bin/env python import fileinput for line in fileinput.input('fansik',inplace=1): line = line.replace(') print line, inplace=1相当于sed的-i参数,如果不加inplace=1,则只答应修改内容,不修改文件 #!/usr/bin/env python import fileinput for line in fileinput.input('fansik',
一.replace()函数1用字符串本身的replace方法: a = 'hello word' b = a.replace('word','python') print b 1 2 3 二.re.sub() import re a = 'hello word' strinfo = re.compile('word') b = strinfo.sub('python',a) print b
windows 默认换行符为 \r\n; unix默认换行符为 \n; 所以当win下编辑的脚本在linux下显示末尾多了^M: 换行符修改为同一的unix格式脚本如下: def run(path,file): for file in files: file = path+'\\'+file f = open(file,'r') result = f.read() print result result = result.replace(r'\r\n',r'\n') f.close() # 需要
f = open("1.txt", "r", encoding="utf-8") f_new = open("2.txt", "w", encoding="utf-8") find_str = "wangzai" replace_str = "doubi" for line in f: if find_str in line: line = lin
Python之父Guido在最近一次采访的最后说了啥? 在前些天的一次采访中,被问到Python未来发展方向的时候原文在infoworld,咱们可爱的python老爹Guido是这样说的: One thing I want to point out are the SciPy and NumPy movements. Those people are introducing Python as a replacement for MatLab. It's open source, it's be
一.安装tkinter 在Linux中python默认是不安装Tkinter模块, [root@li250- ~]# python Python (r266:, Feb , ::) [GCC (Red Hat -)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import Tkint