我想在下面的文件中以特定間隔(例如每30分鐘)更改omega的值。該文件在運行時由另一個程序使用。我想慢慢地從0-10斜坡RPM值(線上的最後一個入口)。基本上,下面的文件中的值「5」需要以每30分鐘1次的增量從0-10變化。在python中每20分鐘編輯一個文件中的特定單詞
rotor
{
// Fixed patches (by default they 'move' with the MRF zone)
nonRotatingPatches(); //if something blows up ..fill the non rotating patches between "(baffles, pressureOutlet etc)"
origin origin [0 1 0 0 0 0 0] (0 0 0);
axis axis [0 0 0 0 0 0 0] (0 0 1);
omega omega [0 0 -1 0 0 0 0] 5;
}
我曾嘗試下面的代碼(我不知道該怎麼辦的,每30分鐘部分),而是由file.writelines(data)
方法寫入的數據似乎並沒有納入我作出值的變化。
import os
import sys
import fileinput
with open('MRFzones', 'r') as file:
data = file.readlines()
# The line omega is line 27 in the file.
line = data[26]
word = line.split()
# >>> data[26].split()
# ['omega', 'omega', '[0', '0', '-1', '0', '0', '0', '0]', '5;']
# The part i want to change is the 10th word.
word[9] = '10;'
# Write everything back.
with open('MRFzones', 'w') as file:
file.writelines(data)
這是行不通的 - 在更新的文件中5未被更改爲10。有人可以幫我實施嗎?我可能甚至沒有正確的想法,所以我們可以在必要時從頭開始編寫代碼。
感謝, 蘇雷什
THA nks的響應..我正在學習Python,並試圖使用該程序做一些自動化!是的,我在unix中使用了類似的腳本來做同樣的事情。你的要簡單得多,但是一般情況下應該使用python!? -suresh – Suresh 2011-05-02 08:30:59
@user:我明白了,如果你的目標是學習Python而不是完成任務:)我的答案並不是非常有用:) – 2011-05-02 08:33:41
就像我說的那樣..工作正在完成,但我也想知道如果我不得不使用Python,我將如何去呢? – Suresh 2011-05-02 08:39:03