【问题标题】:Replace string with wildcard in a .txt file with Python使用 Python 将 .txt 文件中的字符串替换为通配符
【发布时间】:2013-04-07 16:48:18
【问题描述】:

我还是脚本新手,不确定完成我想做的事情的最佳方法。这将是我尝试编写的第一个 python 脚本。请注意,我为此使用 Python2.7

我想为一个给定参数的用户编写一个批处理文件。这个论点将是一条路径。这条路径每天都会改变,并用于执行作业测试。我想用Nightly.bat "build path"调用它

这是我想要完成的:

1.批处理文件通过健康检查确保路径存在。
2.批处理文件使用给定的变量执行python文件。
3. Python 文件在 testrun 脚本中找到一个带有构建路径的字符串,并将该路径替换为给定的变量。
4.批处理文件执行testrun selenium脚本。

这里是代码 Python 代码:

test1.txt 内容:

blah
This is a first string

nightly.py 内容:

import sys
import shutil
import os
import re

tf = open('tmp', 'a+')
string = "This is "

with open('test1.txt') as f:
    for line in f.readlines():
            string = re.sub ('This is .*', 'This is a second string', string)

shutil.copy('test1.txt', 'tmp')
tf.write(string)
f.close()
tf.close()

执行 nightly.py 文件后,这是 tmp 文件内容:

blah
This is a first stringThis is a second String

我需要这样做,所以 This is a first stringThis is a second string 替换

最后,tmp文件应该有以下内容:

blah
This is a second string

感谢您继续尝试。

*****************************
* Updated for Kirbyfan64sos *
*****************************

nightly.py 内容:

import sys
import shutil
import os

tf = open('tmp', 'a+')
with open('test1.txt') as f:
    for line in f.readlines():
        if line == 'This is*':
            line = 'This is a second string' 
        tf.write(line)
f.close()
tf.close()
shutil.copy('tmp', 'test1.txt')
os.remove('tmp')

【问题讨论】:

  • 我找不到问题。
  • 帮助具体是什么?您在哪一部分遇到过问题/困难?
  • 编写python文件的语法。

标签: python windows-7 python-2.7


【解决方案1】:

代码应如下所示:

import sys
tf = open('tmp', 'a+')
with open('WP8974_AudioDecode.html') as f:
    for line in f.readlines() do:
        if line == '<td>\\frosty\*</td>':
            line = '<td>\\frosty\' + sys.argv[1] + '</td>' 
        tf.write(line)
f.close()
shutil.copy('tmp', 'WP8974_AudioDecode.html')
os.remove('tmp')

【讨论】:

  • 谢谢回复,我试试。在 if 行部分,不应该是 1 =,因为我们使用的是通配符吗?
  • 我收到以下错误 ----- AttributeError: 'file' object has no attribute 'append'
  • 感谢.write 的更新。问题是这仅在字符串是静态的(没有通配符)时才有效。如果我使用通配符,那么它不会替换字符串。
  • @FiddleFreak:我正在尝试更新它。看看你能不能得到它。查看 re 模块。给我一些麻烦。我遇到了一些麻烦。很快就会给你答复。抱歉耽搁了。
  • 我已尝试使用 re.sub 模块。我仍然无法正确处理。为您更新了到目前为止我所拥有的帖子...
【解决方案2】:

我终于找到了答案……

执行 Nightly.py 之前的 test1.txt:

blah
blah
This is a first string
blah
blah

顺便说一句,使用 notepad++ 的代码中的标签会有所不同

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 second string
blah
blah

【讨论】:

  • 恭喜!对不起,我帮不了太多。遇到了一点 IDE 问题,然后就忘记了。
【解决方案3】:

我的方法如下,对有问题的文件进行操作,无需复制/复制文件:

import re

with open('target_file.txt', 'r') as file: 
    filedata = file.read()

filedata = re.sub('This is.*', 'This is the second string', filedata)

with open('target_file.txt', 'w') as file:
    file.write(filedata)

【讨论】:

    猜你喜欢
    • 2015-11-24
    • 2018-08-29
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-06
    • 2020-09-12
    • 1970-01-01
    相关资源
    最近更新 更多