【问题标题】:How do I read an UTF 8 encoded text file into a text field?如何将 UTF 8 编码的文本文件读入文本字段?
【发布时间】:2013-04-12 18:03:16
【问题描述】:

如何将 UTF-8 编码的文本文件读入文本字段?

【问题讨论】:

标签: utf-8 livecode


【解决方案1】:

在 LiveCode 中使用 Unicode 时,您需要注意 LiveCode 始终使用 UTF-16 编码文本(格式取决于代码运行的处理器:大端或小端)。因此,要读取 UTF-8 编码的文本文件并显示它,您需要先将数据转换为 UTF-16。

在读取文件时,您需要确保 LiveCode 不会自动为您转换行尾。最简单的方法是使用带有URL 关键字和binfile: 前缀文件名的put 命令。假设文件的路径在变量 theFilename 中,这将读取文件内容,同时保持数据的完整性:

put URL ("binfile:" & theFilename) into theText

现在您的变量中有数据(仍编码为 utf-8),您需要使用 uniencode 将其转换为 UTF-16。

put uniencode(theText, "utf8") into theText

现在文本被编码为 UTF-16,您可以在字段中显示它。

set the unicodeText of field "SomeField" to theText

【讨论】:

  • 感谢 Trevor 的简明解释。我在您的代码中使用了“加载”测试按钮
  • on mouseUp 987654330 if it <> "" then 987654332 put URL ("binfile:" & theFilename) into theText 987654334 set the unicodeText of field "SomeField" to theText 987654336 --no file was selected, or cancel was pressed 987654338 end if 987654340 @ 跨度>
【解决方案2】:

在字典中阅读以下内容:

useUniCode(属性) uniEnCode(函数) UniDeCode(函数)

事实上,任何名称中包含“uni”的条目。

克雷格·纽曼

【讨论】:

  • 好的,谢谢你,克雷格。我现在看到字典中的 uniEncode 函数有一个用户贡献的评论,它也回答了这个问题。 put url ("binfile:/path/to/file/myUniText.ut8") into tRawTxtset the unicodeText of fld "display" to uniEncode(tRawTxt,"UTF8")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多