gpt4 book ai didi

utf-8 - 在 vbscript 中读取 utf-8 文本文件

转载 作者:行者123 更新时间:2023-12-03 11:49:06 39 4
gpt4 key购买 nike

我有一个保存为 UTF-8 的文本文件,当我尝试读取该文件时,它给了我奇怪的字符而不是正确的字符(它包含中文字符)。我怎样才能让它给我正确的汉字?

Option Explicit

Dim objFSO, strTextFile, strData, strLine, arrLines, aniTextFile, aniData, aniLines, aniLine, objTextFile, fso, inputFileList, listFile, fname
Dim iim1, iret, iret2, iret3, i
Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
strTextFile = "C:\Users\admin\Desktop\ArtistCG\folder.txt"
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
arrLines = Split(strData,vbCrLf)

aniTextFile = "C:\Users\admin\Desktop\ArtistCG\folder-list.txt"
aniData = objFSO.OpenTextFile(aniTextFile,ForReading).ReadAll
aniLines = Split(aniData,vbCrLf)

For i = 0 To UBound(arrLines)
strData = objFSO.OpenTextFile(arrLines(i),ForReading).ReadAll
WScript.Echo strData

Set listFile = objFSO.OpenTextFile(aniLines(i),ForReading)
Do While Not listFile.AtEndOfStream
fName = listFile.ReadLine
WScript.Echo fName
Loop
listFile.Close
Next

最佳答案

From the documentation :

The FSO can read only ASCII text files. You cannot use the FSO to read Unicode files or to read binary file formats such as Microsoft Word or Microsoft Excel.



因为你有奇怪的字符,我想这有点不正确,文件是在一些 8 位 Windows 代码页中读取的,因为如果它真的只能读取 ASCII,你会看到 ????
无论如何,如果您可以使用 ADO , 你可以这样做:
Dim objStream, strData

Set objStream = CreateObject("ADODB.Stream")

objStream.CharSet = "utf-8"
objStream.Open
objStream.LoadFromFile("C:\Users\admin\Desktop\ArtistCG\folder.txt")

strData = objStream.ReadText()

objStream.Close
Set objStream = Nothing

关于utf-8 - 在 vbscript 中读取 utf-8 文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13851473/

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