gpt4 book ai didi

excel - 替换文本文件中的文本

转载 作者:行者123 更新时间:2023-12-04 20:23:39 25 4
gpt4 key购买 nike

我创建了一个包含不同部分的文本文件。我需要稍后提取信息。
我将 txt 文件的每一行拆分为每个变量。
例子:

[Title]The Maps[TitleEnd]  
[Name]Smith,John[NameEnd]
[Subject]Mythical Creatures[SubjectEnd]
[Text]Today i learned about this information and blah blah blah[TextEnd]
etc.
我需要打开 txt 文件,找到正确的括号,即 [Text] 到 [TextEnd] 然后替换它。
例子:
[Text]Today i learned about this information and blah blah blah[TextEnd]

[Text]Today i learned about this information and  i learned more things and blah blah blah[TextEnd]
我看了几个例子。一个让我接近但不是替换 1 行,而是将所有内容放在 1 行上。
Private Sub SavePro_Click()

IRBNum = ThisDocument.IRBNetID
FilePath = "Abstract.txt"
TextFile = FreeFile
Open FilePath For Input As #1
StrFinal = "[Text]*"
While EOF(1) = False
Line Input #1, strline
StrFinal = StrFinal + ModifyText(strline)
Wend
StrFinal = Left(StrFinal, Len(StrFinal) - 2)
Close 1
Open FilePath For Output As #1
Print #1, StrFinal
Close #1

End Sub

Function ModifyText(ByVal strInput As String) As String
Dim arrString() As String
Dim strOutput As String

arrString = Split(strInput, " ")
strOutput = arrString(0) + " " + SubText.Text
ModifyText = strOutput

End Function

最佳答案

如果您使用 XML 格式

<xml>
<Title>The Maps</Title>
<Name>Smith,John</Name>
<Subject>Mythical Creatures</Subject>
<Text>Today i learned about this information and blah blah blah</Text>
</xml>
大部分代码都是为您编写的。
Option Explicit

Sub update()

Dim xDoc, oNode
Set xDoc = CreateObject("MSXML2.DOMDocument.6.0")
xDoc.Load Path & "\Abstract.xml"

Set oNode = xDoc.SelectSingleNode("xml/Text")
MsgBox "Text: " & oNode.nodeTypedValue
oNode.nodeTypedValue = "Today i learned about this information " & vbCrLf & _
"and i learned more things and blah blah blah"

xDoc.Save Path & "\Abstract.xml"

End Sub

关于excel - 替换文本文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66196802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com