Python查找指定文件中指定格式字符串,求方法

chen_jia_xing 2012-06-13 07:47:15
文件xx.txt

内容
aabbcc字符串1fdasfe
ccddee字符串2FDSAFE

要找第一行中字符串1,用来替换掉第二行中字符串2后存盘
...全文
1751 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
bugs2k 2012-06-14
  • 打赏
  • 举报
回复
zhu@ubuntu:~/python$
zhu@ubuntu:~/python$ python file.py
ProxyHostaaaa=192.168.1.1
ProxyHostbbb=192.168.1.1
zhu@ubuntu:~/python$ cat file.py
#!/usr/bin/env python

import re

d = 'ProxyHostaaaa=192.168.1.1'
t = re.sub(r'ProxyHostaaaa=', 'ProxyHostbbb=', d)
print d
print t

zhu@ubuntu:~/python$

bugs2k 2012-06-14
  • 打赏
  • 举报
回复
zhu@ubuntu:~/python$ python file.py
ProxyHostaaaa=192.168.1.1
ProxyHostbbb192.168.1.1
zhu@ubuntu:~/python$ cat file.py
#!/usr/bin/env python

import re

d = 'ProxyHostaaaa=192.168.1.1'
t = re.sub(r'(ProxyHostaaaa)=', 'ProxyHostbbb', d)
print d
print t

zhu@ubuntu:~/python$
tim_spac_126 2012-06-14
  • 打赏
  • 举报
回复

import glob, os

def getFiles(path, filefile):
''' 返回指定目录、指定文件扩展名的文件列表 '''
return glob.glob(os.path.join(path, '*%s'%fileext))

files = getFiles(r'../log', '.log')
chen_jia_xing 2012-06-14
  • 打赏
  • 举报
回复
非常感谢各位的热心,写一个小小的程序问题一大堆
这不,又有问题了,下面程序读写英文名文件可以运行,怎么中文就不行了啊?

# -*- coding=utf-8 -*-
import os,re,linecache
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath=os.path.join(root,fn)
filetype =fn.split('.')[1]
if filetype in['testcase']:
print('filepath'+rootpath)
new=(linecache.getline(rootpath,13)).rstrip()
print('newtext'+new)
bt='XmlScripts.+createCrossConnections'
a=re.search(bt,new)
ss=(a.group())
print(ss)
newsp=ss.split('.')
print('newtestx:'+newsp[2])
f=open(rootpath,'r+')
d=f.read()
open(rootpath,'w').write(re.sub('testCaseCharacter','<testCaseCharacter>25_NBI/NBI_北向接口/XML/PTN'+newsp[2]+'.'+newsp[2]+'</testCaseCharacter>',d))
f.close()
else:
continue
fun(r'D:\TEST')





Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
filepathD:\TEST\1_创建Static类型的静态Tunnel_节点类型Ingress_环境预置.testcase
newtext <mLogicClass>XmlScripts.HighLineCases.ConnectionControl.createCrossConnections</mLogicClass>
XmlScripts.HighLineCases.ConnectionControl.createCrossConnections
newtestx:ConnectionControl
Traceback (most recent call last):
File "D:\test2.py", line 24, in <module>
fun(r'D:\TEST')
File "D:\test2.py", line 19, in fun
d=f.read()
UnicodeDecodeError: 'gbk' codec can't decode bytes in position 154-155: illegal multibyte sequence
>>>

chen_jia_xing 2012-06-13
  • 打赏
  • 举报
回复
额,楼上的可以

取一个目录下所有log文件,并将指定行替换成该文件中某处的一个字符串.怎么找到那个字符串,见下面ProxyHostbbb=,这个字符串怎么提取出来啊?



import os,re

def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath = os.path.join(root,fn)
filetype=fn.split('.')[1]
if filetype in['log']:
f= open('fg.ini','r+')
d=f.read()
open('fg.ini', 'w').write(re.sub(r'ProxyHostaaaa=', 'ProxyHostbbb=', d))
f.close()
fun(r'E:\workspace\python')
tim_spac_126 2012-06-13
  • 打赏
  • 举报
回复
import os

def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath = os.path.join(root,fn)
print(rootpath)
f=open(rootpath,'r')
f.close()

fun(r'E:\workspace\python')
chen_jia_xing 2012-06-13
  • 打赏
  • 举报
回复
def fun(path):
for root,dirs,files in os.walk(path):
for fn in files:
rootpath=root+fn
print (rootpath)
f=open(rootpath,'r')
f.close()
fun(r'E:\workspace\python')


为啥open里用rootpath不行啊?
panghuhu250 2012-06-13
  • 打赏
  • 举报
回复
没明白。

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧