怎样用python修改另外一个文件的参数,如修改.txt文件的里面某一个参数??
怎样用python语言修改另外一个文件的参数,如修改.txt文件的里面某一个参数,然后还能保存。...
怎样用python语言修改另外一个文件的参数,如修改.txt文件的里面某一个参数,然后还能保存。
展开
展开全部
刚好其他有个问题涉及你的问题,我直接给你贴可以运行的代码,纯手工额:
#coding=utf-8
'''
Created on 2012-6-4
@author: Administrator
@note:
以下是具体代码:
我用的Python2.7
需要在当前文件夹下创建script.py
'''
import wx
import wx.grid
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
#修改位置,在读取的时候获取,在修改文件的时候使用
self.offsetx = 0
self.offsety = 0
self.initGUI()
def initGUI(self):
self.createWidgets()
self.setPrepertise()
self.doyout()
def createWidgets(self):
self.xshow_textCtrl = wx.TextCtrl(self, -1)
self.yshow_textCtrl = wx.TextCtrl(self, -1)
self.change_btn = wx.Button(self, -1, label = u"修改")
self.Bind(wx.EVT_BUTTON, self.onChange, self.change_btn)
def onChange(self, evt):
#获取修改的属性
x = self.xshow_textCtrl.GetLabelText()
y = self.yshow_textCtrl.GetLabelText()
fl = open("script.py", "w")
#找到相应位置修改
fl.seek(self.offsetx)
fl.write("x = %s\n" % x)
fl.seek(self.offsety)
fl.write("y = %s\n" % y)
fl.close()
def doyout(self):
mainSizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(mainSizer)
#x显示
xSizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer.Add(xSizer, 1, wx.EXPAND)
x_label = wx.StaticText(self, -1, label = "X:")
xSizer.Add(x_label, 0, wx.ALL, 10)
xSizer.Add(self.xshow_textCtrl, 0, wx.ALL, 10)
#y显示
ySizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer.Add(ySizer, 1, wx.EXPAND)
y_label = wx.StaticText(self, -1, label = "Y:")
ySizer.Add(y_label, 0, wx.ALL, 10)
ySizer.Add(self.yshow_textCtrl, 0, wx.ALL, 10)
#按键
mainSizer.Add(self.change_btn, 0, wx.ALL, 10)
def setPrepertise(self):
x, y = self.getXY()
self.xshow_textCtrl.SetLabel(str(x))
self.yshow_textCtrl.SetLabel(str(y))
def getXY(self):
fl = open("script.py", "r")
msg = ""
line = fl.readline()
while line and line[-1]:
if line.startswith("x"):
msg += line
self.offsetx = fl.tell() - len(line)#得到x那行的起始位置
elif line.startswith("y"):
msg += line
self.offsety = fl.tell() - len(line)#得到y那行的起始位置
line = fl.readline()
x = 0
y = 0
exec msg#给x,y赋值
return x, y
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, None, title = u"测试Frame", size = (400, 300),
pos = (400, 200))
TestPanel(self)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()
#coding=utf-8
'''
Created on 2012-6-4
@author: Administrator
@note:
以下是具体代码:
我用的Python2.7
需要在当前文件夹下创建script.py
'''
import wx
import wx.grid
class TestPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
#修改位置,在读取的时候获取,在修改文件的时候使用
self.offsetx = 0
self.offsety = 0
self.initGUI()
def initGUI(self):
self.createWidgets()
self.setPrepertise()
self.doyout()
def createWidgets(self):
self.xshow_textCtrl = wx.TextCtrl(self, -1)
self.yshow_textCtrl = wx.TextCtrl(self, -1)
self.change_btn = wx.Button(self, -1, label = u"修改")
self.Bind(wx.EVT_BUTTON, self.onChange, self.change_btn)
def onChange(self, evt):
#获取修改的属性
x = self.xshow_textCtrl.GetLabelText()
y = self.yshow_textCtrl.GetLabelText()
fl = open("script.py", "w")
#找到相应位置修改
fl.seek(self.offsetx)
fl.write("x = %s\n" % x)
fl.seek(self.offsety)
fl.write("y = %s\n" % y)
fl.close()
def doyout(self):
mainSizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(mainSizer)
#x显示
xSizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer.Add(xSizer, 1, wx.EXPAND)
x_label = wx.StaticText(self, -1, label = "X:")
xSizer.Add(x_label, 0, wx.ALL, 10)
xSizer.Add(self.xshow_textCtrl, 0, wx.ALL, 10)
#y显示
ySizer = wx.BoxSizer(wx.HORIZONTAL)
mainSizer.Add(ySizer, 1, wx.EXPAND)
y_label = wx.StaticText(self, -1, label = "Y:")
ySizer.Add(y_label, 0, wx.ALL, 10)
ySizer.Add(self.yshow_textCtrl, 0, wx.ALL, 10)
#按键
mainSizer.Add(self.change_btn, 0, wx.ALL, 10)
def setPrepertise(self):
x, y = self.getXY()
self.xshow_textCtrl.SetLabel(str(x))
self.yshow_textCtrl.SetLabel(str(y))
def getXY(self):
fl = open("script.py", "r")
msg = ""
line = fl.readline()
while line and line[-1]:
if line.startswith("x"):
msg += line
self.offsetx = fl.tell() - len(line)#得到x那行的起始位置
elif line.startswith("y"):
msg += line
self.offsety = fl.tell() - len(line)#得到y那行的起始位置
line = fl.readline()
x = 0
y = 0
exec msg#给x,y赋值
return x, y
class TestFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, None, title = u"测试Frame", size = (400, 300),
pos = (400, 200))
TestPanel(self)
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = TestFrame()
frame.Show()
app.MainLoop()
展开全部
#!/bin/env python
#the source content in text.txt is "testabc"
#the destination content in text.txt is "testdef"
#open the text file and get the content
f=open("text.txt","r")
content=f.read()
print content
#replace "abc" with "def"
content=content.replace("abc","def")
print content
f.close()
#write the destination content to text.txt
f=open('text.txt','w')
f.write(content)
f.close()
#the source content in text.txt is "testabc"
#the destination content in text.txt is "testdef"
#open the text file and get the content
f=open("text.txt","r")
content=f.read()
print content
#replace "abc" with "def"
content=content.replace("abc","def")
print content
f.close()
#write the destination content to text.txt
f=open('text.txt','w')
f.write(content)
f.close()
追问
可以加一下您的QQ吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
读取这个txt的内容
用正则规换 ,匹配你要替换的参数替换
重新保存
用正则规换 ,匹配你要替换的参数替换
重新保存
追问
我可以加您QQ吗?跟您请教一下。谢谢了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询