【问题标题】:Editing python file library and packges with VB.NET使用 VB.NET 编辑 python 文件库和包
【发布时间】:2014-04-16 09:58:08
【问题描述】:

我有一些 python 文件,我想更改它们的标题:

import os
import math

我需要在导入之前添加目录,例如:

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

(我有目录列表)

我知道如何使用流式阅读器阅读。有人在这里帮助如何获取列表。但现在我想粘贴目录或包。那该怎么办呢?

【问题讨论】:

  • vb.net 在这里似乎是错误的工具。使用 Python 更容易!

标签: python vb.net file io stream


【解决方案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

【讨论】:

  • 但我必须根据导入的类添加不同的目录。所以我还必须阅读所有导入的课程。然后我必须在导入之前添加相应的目录(从列表中提供)。有什么建议吗? @劳伦斯
  • 好吧,我提供的代码使您能够做到这一点。当import 变量中有import 行时,您可以找到相应的模块并从列表中附加相应的目录。您可以使用Contains() 函数或Regex 来检查该行中导入的模块。你可以用它做任何你想做的事情。
猜你喜欢
  • 2022-01-26
  • 1970-01-01
  • 2016-05-23
  • 1970-01-01
  • 2023-04-04
  • 2021-09-28
  • 2015-01-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多