转义非 ASCII 字符(或者如何删除 BOM?)
我需要从输出为 JSON 和 YAML 的 Access 记录集创建一个 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
I need to create an ANSI text file from an Access recordset that outputs to JSON and YAML. I can write the file, but the output is coming out with the original characters, and I need to escape them. For example, an umlaut-O (ö) should be "\u00f6".
I thought encoding the file as UTF-8 would work, but it doesn't. However, having looked at the file coding again, if you write "UTF-8 without BOM" then everything works.
Does anyone know how to either
a) Write text out as UTF-8 without BOM, or
b) Write in ANSI but escaping the non-ASCII characters?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
...好吧...我找到了一些关于如何删除 BOM 的示例代码。我本以为在实际编写文本时可以更优雅地做到这一点。没关系。以下代码删除 BOM。
(本文最初由 Simon Pedersen 发布于 http:// www.imagemagick.org/discourse-server/viewtopic.php?f=8&t=12705)
它可能对其他人有用。
... ok .... i found some example code on how to remove the BOM. I would have thought it would be possible to do this more elegantly when actually writing the text in the first place. Never mind. The following code removes the BOM.
(This was originally posted by Simon Pedersen at http://www.imagemagick.org/discourse-server/viewtopic.php?f=8&t=12705)
It might be useful for someone else.
虽然我已经晚了,但我并不是唯一一个厌倦了我的 SQL 导入被带有字节顺序标记的文本文件破坏的编码员。很少有“堆栈问题”涉及到这个问题 - 这是最接近的问题之一 - 所以我在这里发布一个重叠的答案。
我说“重叠”是因为下面的代码解决的问题与您的问题略有不同 - 主要目的是为具有异构文件集合的文件夹编写架构文件 - 但 BOM 处理段已明确标记。
关键功能是我们迭代文件夹中的所有“.csv”文件,并使用前四个字节的快速半字节测试每个文件:并且仅在看到字节顺序标记时才删除字节顺序标记。
之后,我们将使用原始 C 语言的低级文件处理代码。我们必须一直使用字节数组,因为 您在 VBA 中执行的所有其他操作都会存储字节顺序标记嵌入到字符串变量的结构中。
因此,无需进一步使用 adodb,代码如下:
schema.ini 文件中文本文件的 BOM 处理代码:
如果您知道可以将字节数组分配给 VBA.String,反之亦然,则代码会更容易理解。 BigReplace() 函数是一个 hack,它回避了一些 VBA 低效的字符串处理,尤其是分配:如果您以其他方式执行,您会发现大文件会导致严重的内存和性能问题。
Late to the game here, but I can't be the only coder who got got fed up with my SQL imports being broken by text files with a Byte Order Marker. There are very few 'Stack questions that touch on the problem - this is one of closest - so I'm posting an overlapping answer here.
I say 'overlapping' because the code below is solving a slightly different problem to yours - the primary purpose is writing a Schema file for a folder with a heterogeneous collection of files - but the BOM-handling segment is clearly marked.
The key functionality is that we iterate through all the '.csv' files in a folder, and we test each file with a quick nibble of the first four bytes: and we only only strip out the Byte Order Marker if we see one.
After that, we're working in low-level file-handling code from the primordial C. We have to, all the way down to using byte arrays, because everything else that you do in VBA will deposit the Byte Order Markers embedded in the structure of a string variable.
So, without further adodb, here's the code:
BOM-Disposal code for text files in a schema.ini file:
The code's easier to understand if you know that a Byte Array can be assigned to a VBA.String, and vice versa. The BigReplace() function is a hack that sidesteps some of VBA's inefficient string-handling, especially allocation: you'll find that large files cause serious memory and performance problems if you do it any other way.