0
我需要通過插入一個字符串,用python來更改JavaScript文件的一個特定部分。這是一個JavaScript文件的小樣本。將字符串插入javascript文件的特定部分
//----------------------------------------Peter Lighting Priorities-----------------------------------------------------
/*
// Lighting low prios for everything else//
peter_RenderBoy_lighting_lowPrios = {
"fromProductionGroups" : ["peter_RenderBoy_lighting"],
"matchFilters" : [
{"user": "", "name": ""}
],
"toProductionGroup" : "",
"priority" : 3
};
moveAndPrioritise(peter_RenderBoy_lighting_lowPrios);
// lighting normal and Approved LFL shots (emailed at 8pm) //
peter_RenderBoy_lighting_normalPrios = {
"fromProductionGroups" : ["peter_RenderBoy_lighting"],
"matchFilters" : [
{"user": "", "name": ""}
],
"toProductionGroup" : "",
"priority" : 2
};
moveAndPrioritise(peter_RenderBoy_lighting_normalPrios);
這裏是一段JavaScript同一樣品展示它需要插入字符串「INSERT_STRING_HERE」粗體:
//----------------------------------------Peter Lighting Priorities-----------------------------------------------------
/*
// Lighting low prios for everything else//
peter_RenderBoy_lighting_lowPrios = {
"fromProductionGroups" : ["peter_RenderBoy_lighting"],
"matchFilters" : [
{"user": "", "name": ""}
],
"toProductionGroup" : "",
"priority" : 3
};
moveAndPrioritise(peter_RenderBoy_lighting_lowPrios);
// lighting normal and Approved LFL shots (emailed at 8pm) //
peter_RenderBoy_lighting_normalPrios = {
"fromProductionGroups" : ["peter_RenderBoy_lighting"],
"matchFilters" : [
{"user": "", "name": "***INSERT_STRING_HERE***"}
],
"toProductionGroup" : "",
"priority" : 2
};
moveAndPrioritise(peter_RenderBoy_lighting_normalPrios);
這是我的嘗試,到目前爲止,這僅僅是表演我已經導入了正則表達式,但因爲放棄了我的努力,並試圖循環瀏覽javascript文件,但不知道如何繼續實現我所需要的內容:
import re
javascript_file = open('/depts/wranglers/frankie/farmtools/priorities/autoHook.js')
for line in javascript_file:
if '// lighting normal and Approved LFL shots (emailed at 8pm) //' in line:
Python腳本應該如何知道應該在哪一行插入字符串? – Barmar