gpt4 book ai didi

character-encoding - 将俄语文本写入txt文件

转载 作者:行者123 更新时间:2023-12-02 17:54:01 25 4
gpt4 key购买 nike

我正在尝试将一些俄语文本或西里尔文文本写入 .txt 文件。我可以成功地做到这一点,但是当我打开文件时,所有代替文本写入的内容都是一堆问号。我认为这是一个编码问题,但在该领域找不到任何帮助。我写了一个小脚本来演示这个问题。

do shell script "> $HOME/Desktop/Russian\\ Text.txt"
set text_path to ((path to home folder) & "Desktop:Russian Text.txt" as string) as alias

set write_text to "Привет"

tell application "Finder"
write write_text to text_path
set read_text to text of (read text_path)
end tell

如果有人对为什么会发生这种情况有任何想法,请告诉我。谢谢。

最佳答案

我无法回答你的问题。您的代码中确实有很多 applescript 编码问题,但它们都不会导致您的问题。 Applescript 可以很好地处理非 ASCII 文本。我有时用丹麦语写作,而且很有效。然而,当我使用俄语尝试我的脚本时,我得到了与你相同的结果。我无法解释为什么。只是为了让您可以看到读取和写入文件的正确语法,这是我的代码。请注意,我不使用 Finder 来执行这些任务,并请注意我如何设置输出文件的路径...

set outpath to (path to desktop as text) & "danish.txt"
set theText to "primær"

-- write the file
set openFile to open for access file outpath with write permission
write theText to openFile
close access openFile

-- read the file
set readText to read file outpath

更新:我找到了您问题的答案。看起来,如果您将 utf-16 字节顺序标记 (BOM) 写入文件,那么它可以正常用于俄语。因此,我创建了两个处理程序,以便您可以读取和写入这些文件...

set filePath to (path to desktop as text) & "russian.txt"
set theText to "Привет"

write_UnicodeWithBOM(filePath, theText, true)
read_UnicodeWithBOM(filePath)

on write_UnicodeWithBOM(filePath, theText)
try
set openFile to open for access file (filePath as text) with write permission
write (ASCII character 254) & (ASCII character 255) to openFile starting at 0
write theText to openFile starting at eof as Unicode text
end try
try
close access openFile
end try
end write_UnicodeWithBOM

on read_UnicodeWithBOM(filePath)
read file (filePath as text) as Unicode text
end read_UnicodeWithBOM

关于character-encoding - 将俄语文本写入txt文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4329410/

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