Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3替换行_Python_Python 3.x - Fatal编程技术网

Python3替换行

Python3替换行,python,python-3.x,Python,Python 3.x,我在Python方面有点问题。以下是代码: f = open('/path/to/file', 'r') filedata = f.read() f.close() postgres = filedata.replace('# DBENGINE=MYSQL', 'DBENGINE=PGSQL') dbname = filedata.replace('# DBNAME=DB1', 'DBNAME=DB1') dbrwuser = filedata.replace('# DBRWUSER="user

我在Python方面有点问题。以下是代码:

f = open('/path/to/file', 'r')
filedata = f.read()
f.close()
postgres = filedata.replace('# DBENGINE=MYSQL', 'DBENGINE=PGSQL')
dbname = filedata.replace('# DBNAME=DB1', 'DBNAME=DB1')
dbrwuser = filedata.replace('# DBRWUSER="user1"', 'DBRWUSER="user1"')
f = open('/path/to/file', 'w')
f.write(postgres)
f.write(dbname)
f.write(dbrwuser)
f.close()
正如你所看到的,我试图读取一个大文件,当我试图替换它时,它只是替换了“Postgres”,而没有对“dbname,dbrwuser”等进行更改。因此,我试图找出它,但无法做到

有什么想法或样品吗


谢谢。

您将输入复制三份,而不是每次都替换。使用以下命令:

filedata = filedata.replace('# DBENGINE=MYSQL', 'DBENGINE=PGSQL')
filedata = filedata.replace('# DBNAME=DB1', 'DBNAME=DB1')
filedata = filedata.replace('# DBRWUSER="user1"', 'DBRWUSER="user1"')
...
f.write(filedata)

您还可以通过将所有替换字符串放入一个命令行来完成此操作

import re
repString = {'# DBENGINE=MYSQL': 'DBENGINE=PGSQL', '# DBNAME=DB1': 'DBNAME=DB1', '# DBRWUSER="user1"': 'DBRWUSER="user1"'}

repString = dict((re.escape(k), v) for k, v in repString.iteritems())
pattern = re.compile("|".join(repString.keys()))
filedata = pattern.sub(lambda m: repString[repString.escape(m.group(0))], filedata)
f = open('/path/to/file', 'w')
f.write(filedata)
f.close()

一些建议和澄清:

f.read()
读取整个文件。对于大型文件,这可能不是一个好主意。相反,使用

with open(filename, "r") as f:
    for line in f:
        # do something with the line
在open()中使用
还可以消除以后关闭文件的需要-这是自动完成的

string.replace()
返回第一个参数替换为第二个参数的整个字符串。由于每次使用
replace
时都会创建一个新变量,因此更改仅适用于单个变量。在
postgres
中所做的更改将不存在于
dbname
中。 相反,为每个
replace
重新定义变量
filedata
,以保留更改并避免不必要的复制:

filedata = filedata.replace('# DBENGINE=MYSQL', 'DBENGINE=PGSQL')
filedata = filedata.replace('# DBNAME=DB1', 'DBNAME=DB1')
filedata = filedata.replace('# DBRWUSER="user1"', 'DBRWUSER="user1"')
# at this point, filedata contains all three changes
使用
w
选项打开要写入的文件时,该文件将被覆盖。这意味着文件将只包含上次写入时写入的内容,
f.write(dbrwuser)
。而是进行更改,只写一次或附加到文件:

filedata = filedata.replace('# DBENGINE=MYSQL', 'DBENGINE=PGSQL')
...
...

with open('/path/to/file', 'w') as f:
    f.write(filedata)

对于大文件,避免使用
f.read()
。它将整个文件读入内存<在f:…中,将open(filename)作为f:for行的code>通常更好,因为它一次只能读取一行
string.replace()
返回第一个参数替换为第二个参数的整个字符串。这意味着变量
postgres
dbname
dbrwuser
都包含整个文件的副本,其中只替换了给定的字符串。也就是说,
dbrwuser
中仍然有
#DBENGINE=MYSQL
,而
postgres
中仍然有
'#dbrwuser=“user1”
。另外,
f=open()
中的
w
选项每次都会覆盖该文件。@jDo哇,听起来像是我搞砸了。我应该像“用open(filename)…”这样修改吗?最好的方法是什么?我已经把评论变成了一个答案,并添加了更多的细节。如果有什么不清楚的地方,请随时询问。