Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.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_String_Python 2.7_File Io - Fatal编程技术网

Python 替换环境已知但字符串值未知的字符串

Python 替换环境已知但字符串值未知的字符串,python,string,python-2.7,file-io,Python,String,Python 2.7,File Io,我有一个字符串的形式 **var beforeDate = new Date('2015-08-21')** 这里我不知道parantasis()之间的值。我想用其他日期替换这个日期。我如何用Python实现它? 我曾想过打开该文件,然后使用该语言的标准replace函数,但由于beween()的值未知,所以这是不可能的 这个代码段以及这个代码段之后都会有很多代码,所以用新行替换整行代码是行不通的,因为它会覆盖这个代码段周围的代码。使用正则表达式怎么样?例如: temp.txt print "

我有一个字符串的形式

**var beforeDate = new Date('2015-08-21')**
这里我不知道parantasis()之间的值。我想用其他日期替换这个日期。我如何用Python实现它? 我曾想过打开该文件,然后使用该语言的标准replace函数,但由于beween()的值未知,所以这是不可能的


这个代码段以及这个代码段之后都会有很多代码,所以用新行替换整行代码是行不通的,因为它会覆盖这个代码段周围的代码。

使用正则表达式怎么样?例如:

temp.txt

print "I've got a lovely bunch of coconuts"
var beforeDate = new Date('2015-08-21') #date determined by fair die roll
print "Here they are, standing in a row"
main.py

import re

new_value = "'1999-12-31'"
with open("temp.txt") as infile:
    data = infile.read()
    data = re.sub(r"(var beforeDate = new Date\().*?(\))", "\\1"+new_value+"\\2", data)
with open("output.txt", "w") as outfile:
    outfile.write(data)
运行后的output.txt:

print "I've got a lovely bunch of coconuts"
var beforeDate = new Date('1999-12-31') #date determined by fair die roll
print "Here they are, standing in a row"

使用正则表达式怎么样?例如:

temp.txt

print "I've got a lovely bunch of coconuts"
var beforeDate = new Date('2015-08-21') #date determined by fair die roll
print "Here they are, standing in a row"
main.py

import re

new_value = "'1999-12-31'"
with open("temp.txt") as infile:
    data = infile.read()
    data = re.sub(r"(var beforeDate = new Date\().*?(\))", "\\1"+new_value+"\\2", data)
with open("output.txt", "w") as outfile:
    outfile.write(data)
运行后的output.txt:

print "I've got a lovely bunch of coconuts"
var beforeDate = new Date('1999-12-31') #date determined by fair die roll
print "Here they are, standing in a row"

就我个人而言,我通常发现re.split()比re.sub()更易于使用。这将重用Kevin的代码,并将捕获它所做的一切(加上中间组),然后替换中间组:

import re

new_value = "'1999-12-31'"
with open("temp.txt") as infile:
    data = infile.read()

data = re.split(r"(var beforeDate = new Date\()(.*?)(\))", data)
# data[0] is everything before the first capture
# data[1] is the first capture
# data[2] is the second capture -- the one we want to replace
data[2] = new_value

with open("output.txt", "w") as outfile:
    outfile.write(''.join(stuff))
你可以放弃抓取中间组,但你会在列表中插入内容。换一个更容易

OTOH,这个问题可能很小,不需要重新锤击。以下是相同的代码,无需重复:

new_value = "'1999-12-31'"
with open("temp.txt") as infile:
    data = infile.read()

data = list(data.partition('var beforeDate = new Date('))
data += data.pop().partition(')')
data[2] = new_value

with open("output.txt", "w") as outfile:
    for stuff in data:
        outfile.write(stuff)

就我个人而言,我通常发现re.split()比re.sub()更易于使用。这将重用Kevin的代码,并将捕获它所做的一切(加上中间组),然后替换中间组:

import re

new_value = "'1999-12-31'"
with open("temp.txt") as infile:
    data = infile.read()

data = re.split(r"(var beforeDate = new Date\()(.*?)(\))", data)
# data[0] is everything before the first capture
# data[1] is the first capture
# data[2] is the second capture -- the one we want to replace
data[2] = new_value

with open("output.txt", "w") as outfile:
    outfile.write(''.join(stuff))
你可以放弃抓取中间组,但你会在列表中插入内容。换一个更容易

OTOH,这个问题可能很小,不需要重新锤击。以下是相同的代码,无需重复:

new_value = "'1999-12-31'"
with open("temp.txt") as infile:
    data = infile.read()

data = list(data.partition('var beforeDate = new Date('))
data += data.pop().partition(')')
data[2] = new_value

with open("output.txt", "w") as outfile:
    for stuff in data:
        outfile.write(stuff)


这是在一个文件中只出现一次,还是多次出现?同一个日期是否在多个位置使用?一旦它在变量beforeDate=new date('date_I_'u want_to_Put')中初始化,然后它的值将在特定位置使用以进行比较,如果某个日期>beforeDate…您是否考虑过/放弃使用正则表达式(re模块)来查找要替换的字符串?“我不知道这两者之间的价值()“。你知道价值的结构吗,即使你不知道价值本身?例如,它是否总是一个用单引号括起来的字符串文字?@James:是的,正则表达式对我来说似乎也是最好的选择,我不知道如何使用它们,虽然这个东西在一个文件中只出现一次,还是出现过几次?同一个日期是否在多个位置使用?一旦它在变量beforeDate=new date('date_I_'u want_to_Put')中初始化,然后它的值将在特定位置使用以进行比较,如果某个日期>beforeDate…您是否考虑过/放弃使用正则表达式(re模块)来查找要替换的字符串?“我不知道这两者之间的价值()“。你知道价值的结构吗,即使你不知道价值本身?例如,它是否总是一个用单引号括起来的字符串文字?@James:是的,正则表达式对我来说似乎也是最好的选择,我不知道如何使用它们,虽然你为什么要用open(“output.txt”,“w”)作为outfile:缩进这一行,它不会与前面的缩进级别相同吗?目光敏锐。带有的第二个
确实不需要缩进到第一个
中。已编辑。“虽然它在您提供的示例代码上运行良好,但在我的代码上它不起作用。一行\n var afterDate=new Date('2015-08-19');已转换为\n P15-08-13);只需尝试更改代码new_value=“1999-12-31“,我刚刚删除了新_值中的单引号。很有趣。。。你能放一个示例文档在我自己的机器上复制这个问题吗?为什么你用open(“output.txt”,“w”)将行缩进为outfile:,它不会与前面的缩进级别相同吗?目光敏锐。带有
的第二个
确实不需要缩进到第一个
中。已编辑。“虽然它在您提供的示例代码上运行良好,但在我的代码上它不起作用。一行\n var afterDate=new Date('2015-08-19');已转换为\n P15-08-13);只需尝试更改代码new_value=“1999-12-31“,我刚刚删除了新_值中的单引号。很有趣。。。你能给我一个示例文档,这样我就可以在我自己的机器上复制这个问题吗?你的方法非常巧妙:)+1你的方法非常巧妙:)+1