2014-04-16 31 views
0

我有一些Python文件,我想改變自己的頭:編輯Python文件庫和packges用VB.NET

import os 
import math 

我需要像導入之前添加目錄:

from com.firebox import os 
from com.fireboxtraining import math 

(我有導演列表)

我知道如何閱讀流讀者。有人在這裏幫助瞭如何獲得清單。但是現在我想粘貼目錄或包。那麼該怎麼辦?

+0

vb.net好像這裏的錯誤的工具。比方說,Python更容易! –

回答

1

Python文件只是普通文件,你可以像閱讀任何文本文件一樣閱讀。 您需要做的是打開文件,檢查包含導入的行,更改這些行,然後將所有行寫入新文件。
爲了讓您一開始,這裏是與被冷落爲你找出份樣機:

Dim file As System.IO.File 
Dim reader as System.IO.StreamReader 
Dim line As String 
reader = file.OpenText("ThePythonFile.py") 

'open a new file for writing the results here 

While reader.Peek <> -1 
    'get lines one by one 
    line = reader.ReadLine() 

    'check to see if it has import. this is just an example, 
    'you should have a more complicated check here, like regex 
    If line.Contains("import") Then 
     'alter the line as you wish 
    End If 

    'Write the line to the new file 

End While 
reader.Close() 

'close the second file 
'If you want, you can replace the old file with the new one here 
+0

但我必須根據導入的類添加不同的目錄。所以我也必須讀取所有導入的類。那麼我必須在導入之前添加相應的目錄(從列表中提供)。任何建議? @勞倫斯 – istiaq2379

+1

那麼,我提供的代碼可以讓你做到這一點。當你在'line'變量中有'import'行時,你可以找到合適的模塊並從列表中追加相應的目錄。你可以使用'Contains()'函數或者'Regex'來檢查在這行中導入的模塊。那麼你可以隨心所欲地做任何事情。 – bosnjak