我正在尝试用相应的字符串部分自动替换文件中的源代码文本行。 为此,我创建了一个工作流,以获得两个数据结构,其中包含特定行的所有相应文本。但是,由于我的源代码文件是在coffee脚本中(空格很重要!)下面的代码无法检测行中的字符串(因为代码部分开头有可变的空格)。有没有一种方法可以使用通配符或保留空白并进行替换:
# -*- coding: utf-8 -*-
import fileinput
# List of strings to be replaced in the source
sourceList = ['name: "My House",','name: "His House",']
# List of target translated strings matching index values with the sourceList
targetList = ['name: "私の家",','name: "彼の家",']
# Loops over sourceList and performs in-place replacement with the value at corresponding index in targetList
for i, val in enumerate(sourceList):
for line in fileinput.input("seed-staging.coffee", inplace=True):
print(line.replace(sourceList[i], targetList[i]), end = '') # End part is essential to not get default newline from print
目的是找到行,替换它保留左侧缩进级别的空白。
更新:为Coffee脚本添加源和所需的输出示例。你知道吗
exports.locations = [
{
"name": "My House",
...
}
应该转向:
exports.locations = [
{
"name": "私の家",
...
}
关键是要保留空白。你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐