gpt4 book ai didi

ms-access - 转义非 ASCII 字符(或者如何删除 BOM?)

转载 作者:行者123 更新时间:2023-12-02 22:28:08 25 4
gpt4 key购买 nike

我需要从 Access 记录集创建一个输出为 JSON 和 YAML 的 ANSI 文本文件。我可以写入文件,但输出是原始字符,我需要转义它们。例如,元音变音-O (ö) 应为“\u00f6”。

我认为将文件编码为 UTF-8 会起作用,但事实并非如此。但是,再次查看文件编码后,如果您编写“UTF-8 without BOM”,则一切正常。

有谁知道怎么做吗

a) 将文本写为不带 BOM 的 UTF-8,或者b) 用 ANSI 编写但转义非 ASCII 字符?

Public Sub testoutput()

Set db = CurrentDb()

str_filename = "anothertest.json"
MyFile = CurrentProject.Path & "\" & str_filename
str_temp = "Hello world here is an ö"

fnum = FreeFile

Open MyFile For Output As fnum
Print #fnum, str_temp
Close #fnum

End Sub

最佳答案

...好的....我找到了一些关于如何删除 BOM 的示例代码。我本以为在实际编写文本时可以更优雅地做到这一点。没关系。以下代码删除 BOM。

(本文最初由 Simon Pedersen 发布于 http://www.imagemagick.org/discourse-server/viewtopic.php?f=8&t=12705 )

' Removes the Byte Order Mark - BOM from a text file with UTF-8 encoding
' The BOM defines that the file was stored with an UTF-8 encoding.

Public Function RemoveBOM(filePath)

' Create a reader and a writer
Dim writer, reader, fileSize
Set writer = CreateObject("Adodb.Stream")
Set reader = CreateObject("Adodb.Stream")

' Load from the text file we just wrote
reader.Open
reader.LoadFromFile filePath

' Copy all data from reader to writer, except the BOM
writer.Mode = 3
writer.Type = 1
writer.Open
reader.Position = 5
reader.CopyTo writer, -1

' Overwrite file
writer.SaveToFile filePath, 2

' Return file name
RemoveBOM = filePath

' Kill objects
Set writer = Nothing
Set reader = Nothing
End Function

这可能对其他人有用。

关于ms-access - 转义非 ASCII 字符(或者如何删除 BOM?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2317605/

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