Python 2.7 如何用通配符替换使用python的文件行
我是python新手,正在使用2.7版 我想使用填充该行其余部分的Python 2.7 如何用通配符替换使用python的文件行,python-2.7,windows-7-x64,Python 2.7,Windows 7 X64,我是python新手,正在使用2.7版 我想使用填充该行其余部分的通配符替换文件中的字符串。这是我到目前为止所拥有的 这将起作用,并将文件字符串更改为第二个字符串: if line == 'This = first string': line = 'This = second string' if line == 'This = *': line = 'This = second string' 这将不起作用,文件字符串将保留为第一个字符串: if line == 'This
通配符替换文件中的字符串。这是我到目前为止所拥有的
这将起作用,并将文件字符串更改为第二个字符串
:
if line == 'This = first string':
line = 'This = second string'
if line == 'This = *':
line = 'This = second string'
这将不起作用,文件字符串将保留为第一个字符串
:
if line == 'This = first string':
line = 'This = second string'
if line == 'This = *':
line = 'This = second string'
完整脚本:
import sys
import shutil
import os
import re
#Assigns tf as a tmp file with the module for appending and writing, and creating the file if it does not exist.
tf = open('tmp', 'a+')
#Assigns f as test.txt
with open('test1.txt') as f:
#Reads the line in the test1.txt file
for line in f.readlines():
#Checks for the line condition
if line == 'This = *':
#Sets the new line condition
line = 'This = second string'
#Replaces the line withe the new build path
tf.write(line)
#Closes the test2.txt file
f.close()
tf.close()
#Copies the changes from the tmp file and replaces them into the test2.txt
shutil.copy('tmp', 'test2.txt')
#Removies the tmp file from the computer
os.remove('tmp')
J.F.塞巴斯蒂安的最新代码
test2.txt内容:
blah
This = first string
import os
from tempfile import NamedTemporaryFile
prefix = "This = "
path = 'test2.txt'
dirpath = os.path.dirname(path)
with open(path) as input_file:
with open(path+".tmp", mode="w") as tmp_file:
for line in input_file:
if line.startswith(prefix):
line = prefix + "second string\n"
tmp_file.write(line)
tmp_file.delete = False
os.remove(path)
os.rename(tmp_file.name, path)
testing2.py内容:
blah
This = first string
import os
from tempfile import NamedTemporaryFile
prefix = "This = "
path = 'test2.txt'
dirpath = os.path.dirname(path)
with open(path) as input_file:
with open(path+".tmp", mode="w") as tmp_file:
for line in input_file:
if line.startswith(prefix):
line = prefix + "second string\n"
tmp_file.write(line)
tmp_file.delete = False
os.remove(path)
os.rename(tmp_file.name, path)
错误消息:
C:\Users\james>testing2.py
Traceback (most recent call last):
File "C:\Users\james\testing2.py", line 13, in <module>
tmp_file.delete = False
AttributeError: 'file' object has no attribute 'delete'
C:\Users\james>testing2.py
回溯(最近一次呼叫最后一次):
文件“C:\Users\james\testing2.py”,第13行,在
tmp_file.delete=False
AttributeError:“文件”对象没有属性“删除”
有什么建议吗?替换以前缀开头的行:
prefix = "This = "
if line.startswith(prefix):
line = prefix + "second string"
如果该文件是带有key=value
行的配置文件,则还可以使用或正则表达式替换该值(在更复杂的情况下):
替换文件中的行;您可以使用:
我终于找到了答案 执行Nightly.py之前的test1.txt:
blah
blah
This is a first string
blah
blah
blah
blah
This is a second string
blah
blah
顺便说一句,使用记事本时,标签会对代码产生影响++
import sys
import os
import re
import shutil
tf = open('tmp', 'a+')
with open('test1.txt') as f:
for line in f.readlines():
build = re.sub ('This is.*','This is a second string',line)
tf.write(build)
tf.close()
f.close()
shutil.copy('tmp', 'test1.txt')
os.remove('tmp')
执行Nightly.py后的test1.txt:
blah
blah
This is a first string
blah
blah
blah
blah
This is a second string
blah
blah
我得到一个错误
前缀或行应为缩进块。我更新了完整的脚本,也许你可以更清楚地了解picture@FiddleFreak:使用完整回溯发布引发错误的代码。检查你是否没有混合制表符和空格。发布它,等待回复…@FiddleFreak:我已经添加了完整的代码示例。仍然不起作用。最后一行出现错误WindowsError:[错误2]系统找不到指定的文件
您试图修复的属性错误是什么?您希望tmp\u file.delete=False
行做什么?为什么在那里?