Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.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
Python 无法获取变量。替换工作正常_Python - Fatal编程技术网

Python 无法获取变量。替换工作正常

Python 无法获取变量。替换工作正常,python,Python,我试图用python文件中的新字符串替换字符串,并将新字符串永久写入其中。当我运行下面的脚本时,它会删除字符串的一部分,而不是全部。文件中的字符串为: self.id = "027FC8EBC2D1" 我必须替换字符串的脚本是: def edit(): o = open("test.py","r+") #open for line in open("test.py"): line = line.replace("027FC8EBC2D1","NewVa

我试图用python文件中的新字符串替换字符串,并将新字符串永久写入其中。当我运行下面的脚本时,它会删除字符串的一部分,而不是全部。文件中的字符串为:

self.id = "027FC8EBC2D1"
我必须替换字符串的脚本是:

def edit():

    o = open("test.py","r+") #open 
    for line in open("test.py"):   
        line = line.replace("027FC8EBC2D1","NewValue")  
        o.write(line) 
    o.close()

edit()

谢谢您的帮助。

您正在以只读方式打开文件并尝试写入。你还可以一次打开两次


您需要重新组织它,以便只打开一次,并且它是读写的。

尝试以下方法:

import fileinput
for line in fileinput.input('test.py', inplace=1):
  line.replace("027FC8EBC2D1","NewValue")

这使得您对fh.close()的调用无关(由处理它的)并防止您一次打开文件的多个副本。

除非替换值和原始值具有完全相同的长度,否则您无法安全地执行您想要执行的操作。除非有保证,否则我会复制文件:

with open('input.txt', 'r') as in_file:
    with open('output.txt', 'w') as out_file:
        for line in in_file:
           line = line.replace('027FC8EBC2D1', 'NewValue')
           out_file.write(line)
编辑(删除误导性信息)

使用Python对文本文件执行(实际模拟;-)“就地替换”的正确方法是模块:

请注意,与另一个建议相同模块的答案相比,有两个关键的细节:
input
的第一个参数必须是文件名列表(而不是字符串!),并且,必须
打印结果文件中所需的每一行(
fileinput
重定向标准输出以执行——实际模拟——“就地覆盖”效果)


最后一个小但不可忽略的细节:
print
语句末尾的逗号是为了避免在末尾添加另一个换行符(因为每个
都已以换行符结尾!-)。

如果与文件大小相比,您有足够的内存,您实际上可以解决以下问题:

# Open the file for read/write access
f = open( 'test.py', 'r+' )

# Read the entire contents of the file into memory
data = f.read()

# Replace the old value with the new one
data.replace( '027FC8EBC2D1', 'NewValue' )

# Truncate the file (in case NewValue is shorter than OldValue)
f.truncate( 0 )

# Write all the data out again
f.write( data )

# Close the file
f.close()

我不建议将其用于非常大的文件,但这将是一个比您预期的更快的解决方案。

此代码运行后文件的内容是什么?是的,这有点令人困惑,因为有两个
open
语句-
for
循环一是只读的,但他写入的是
r+
.t他说,打开两个文件句柄的混乱性质意味着它可能只需要一个reorg@eksortso谢谢!我只是从2.6开始使用Python。我不知道之前必须手动导入带有的
。我尝试了这段代码,但出现了一个错误,说string对象没有write()。我将其更改为fh.write(line)。但这会将新值附加到文件中。@chrissygormley您是对的,它的行为与我预期的不完全一样。我用了一个使用fileinput模块的示例进行了更新。谢谢,
fileinput
是一个很好的方法。但是您应该
打印您生成的字符串,
rstrip
该字符串,或者以其他方式打印它t不使用行分隔符,并为
输入
提供
备份
参数,以防输出不正确,需要恢复原始文件。
包含行尾字符,无需再次添加。
# Open the file for read/write access
f = open( 'test.py', 'r+' )

# Read the entire contents of the file into memory
data = f.read()

# Replace the old value with the new one
data.replace( '027FC8EBC2D1', 'NewValue' )

# Truncate the file (in case NewValue is shorter than OldValue)
f.truncate( 0 )

# Write all the data out again
f.write( data )

# Close the file
f.close()