ænd*_*rük 5 workflow nautilus encoding poster
PosteRazor使用明显过时的 GUI,无法正确显示我的文件名:
为方便起见,我希望能够通过从 Nautilus 复制和粘贴其路径来在 PosteRazor 中打开任何文件。这适用于其他应用程序,但遗憾的是,PosteRazor 无法理解路径:
如何将 Nautilus 生成的路径转换为与 PosteRazor 兼容的文本编码?
PosteRazor 的 Ubuntu 包列出了对 Fast Light Toolkit (FLTK) 的依赖。它关于 Unicode 的程序员文档看起来可能包含回答我的问题的必要信息,但我不确定如何解释它。
一些示例内容:
Nautilus 中原生出现的路径:
/home/ak/café/north-america.jpg
Run Code Online (Sandbox Code Playgroud)与它在 PosteRazor 中原生出现的路径相同:

从 Nautilus 复制路径后的剪贴板内容:
$ xclip -out -selection clipboard -target TARGETS
TIMESTAMP
TARGETS
MULTIPLE
x-special/gnome-copied-files
text/uri-list
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/plain;charset=utf-8
text/plain
$ xclip -out -selection clipboard -target STRING | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 e9 2f 6e 6f |/home/ak/caf./no|
00000010 72 74 68 2d 61 6d 65 72 69 63 61 2e 6a 70 67 |rth-america.jpg|
0000001f
$ xclip -out -selection clipboard -target UTF8_STRING | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 c3 a9 2f 6e |/home/ak/caf../n|
00000010 6f 72 74 68 2d 61 6d 65 72 69 63 61 2e 6a 70 67 |orth-america.jpg|
00000020
$ xclip -out -selection clipboard -target text/plain | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 5c 75 30 30 |/home/ak/caf\u00|
00000010 65 39 2f 6e 6f 72 74 68 2d 61 6d 65 72 69 63 61 |e9/north-america|
00000020 2e 6a 70 67 |.jpg|
00000024
$ xclip -out -selection clipboard -target 'text/plain;charset=utf-8' | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 c3 a9 2f 6e |/home/ak/caf../n|
00000010 6f 72 74 68 2d 61 6d 65 72 69 63 61 2e 6a 70 67 |orth-america.jpg|
00000020
Run Code Online (Sandbox Code Playgroud)从 PosteRazor 复制路径后的剪贴板内容:
$ xclip -out -selection clipboard -target TARGETS
STRING
$ xclip -out -selection clipboard -target STRING | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 c3 a9 2f 6e |/home/ak/caf../n|
00000010 6f 72 74 68 2d 61 6d 65 72 69 63 61 2e 6a 70 67 |orth-america.jpg|
00000020
Run Code Online (Sandbox Code Playgroud)从 Nautilus 复制路径并将其粘贴到 PosteRazor 后的 PosteRazor:

从 PosteRazor 复制路径并将其粘贴到 PosteRazor 后的 PosteRazor:

从 PosteRazor 复制并粘贴到 Chromium 的路径:
/home/ak/café/norrth-america.jpg
Run Code Online (Sandbox Code Playgroud)从 PosteRazor 复制并粘贴到 Chromium 中的路径,然后从 Chromium 复制并粘贴回 PosteRazor:

从 Chromium 复制后的剪贴板内容:
$ xclip -out -selection clipboard -target TARGETS
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
COMPOUND_TEXT
STRING
TEXT
UTF8_STRING
text/plain
$ xclip -out -selection clipboard -target STRING | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 c3 a9 2f 6e |/home/ak/caf../n|
00000010 6f 72 72 74 68 2d 61 6d 65 72 69 63 61 2e 6a 70 |orrth-america.jp|
00000020 67 |g|
00000021
$ xclip -out -selection clipboard -target UTF8_STRING | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 c3 a9 2f 6e |/home/ak/caf../n|
00000010 6f 72 72 74 68 2d 61 6d 65 72 69 63 61 2e 6a 70 |orrth-america.jp|
00000020 67 |g|
00000021
$ xclip -out -selection clipboard -target text/plain | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 c3 a9 2f 6e |/home/ak/caf../n|
00000010 6f 72 72 74 68 2d 61 6d 65 72 69 63 61 2e 6a 70 |orrth-america.jp|
00000020 67 |g|
00000021
Run Code Online (Sandbox Code Playgroud)从 PosteRazor 复制并粘贴到 GNOME 终端的路径:

从 PosteRazor 复制并粘贴到 GNOME 终端的路径,然后从 GNOME 终端复制并粘贴回 PosteRazor:

从 GNOME 终端复制后的剪贴板内容:
$ xclip -out -selection clipboard -target TARGETS
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/plain;charset=utf-8
text/plain
$ xclip -out -selection clipboard -target STRING | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 e9 2f 6e 6f |/home/ak/caf./no|
00000010 72 74 68 2d 61 6d 65 72 69 63 61 2e 6a 70 67 |rth-america.jpg|
0000001f
$ xclip -out -selection clipboard -target UTF8_STRING | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 c3 a9 2f 6e |/home/ak/caf../n|
00000010 6f 72 74 68 2d 61 6d 65 72 69 63 61 2e 6a 70 67 |orth-america.jpg|
00000020
$ xclip -out -selection clipboard -target 'text/plain' | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 5c 75 30 30 |/home/ak/caf\u00|
00000010 65 39 2f 6e 6f 72 74 68 2d 61 6d 65 72 69 63 61 |e9/north-america|
00000020 2e 6a 70 67 |.jpg|
00000024
$ xclip -out -selection clipboard -target 'text/plain;charset=utf-8' | hexdump -C
00000000 2f 68 6f 6d 65 2f 61 6b 2f 63 61 66 c3 a9 2f 6e |/home/ak/caf../n|
00000010 6f 72 74 68 2d 61 6d 65 72 69 63 61 2e 6a 70 67 |orth-america.jpg|
00000020
Run Code Online (Sandbox Code Playgroud)更新:可以使用以下命令:
\n\n\nxclip -out -选择剪贴板 -目标 STRING | iconv --来自代码 ISO-8859-15 --至代码 UTF-8 | xclip -in -选择剪贴板\n\n\n
如需解释,请阅读完整答案。
\n\n要完全理解答案,您需要了解 Unicode 代码点和 unicode 编码。
\n\n以下是所需术语的简短定义和解释,但我建议您从答案末尾提到的来源中阅读它们。
\n\nUnicode 代码空间:从 0 到 10FFFF 16的整数范围。
Unicode 代码点: Unicode 代码空间中的任何值。代码点对应于字符,但并非所有代码点都分配给编码字符。
UTF-8: UTF-8(UCS 转换格式 - 8 位)是一种可变宽度编码,可以表示 Unicode 字符集中的每个字符。UCS 代表通用字符集。
\n\n前 128 个字符 (US-ASCII) 需要一个字节。接下来的 1,920 个字符需要两个字节进行编码。这涵盖了几乎所有源自拉丁语的字母表的其余部分,以及希腊语、西里尔语、科普特语、亚美尼亚语、希伯来语、阿拉伯语、叙利亚语和 T\xc4\x81na 字母表,以及组合变音符号。
\n\n\xc3\xa9这表明导致问题的字符需要两个字节才能以 UTF-8 进行编码。我们将使用一些命令来验证它。
ISO/IEC 8859-15: 8 位单字节编码图形字符集。
为了测试,我创建了一个目录/home/green/Pictures/caf\xc3\xa9/。
从 复制位置后nautilus,命令的输出如下:
命令#1:
\n\n\n$ xclip -out -选择剪贴板 -目标 STRING | hexdump -C\n00000000 2f 68 6f 6d 65 2f 67 72 65 65 6e 2f 50 69 63 74 |/home/green/Pict|\n00000010 75 72 65 73 2f 63 61 66 e9 2f |ures/caf./|\n00 00001a \n\n\n
请注意, 的编码caf\xc3\xa9是63 61 66 e9,这是可以的,因为 Unicode 代码点 U+00E9 表示{LATIN SMALL LETTER E WITH ACUTE}或\xc3\xa9。
命令#2:
\n\n\n$ xclip -out -选择剪贴板 -目标 UTF8_STRING | hexdump -C\n00000000 2f 68 6f 6d 65 2f 67 72 65 65 6e 2f 50 69 63 74 |/home/green/Pict|\n00000010 75 72 65 73 2f 63 61 66 c3 a9 2f |ures/caf../| \n0000001b\n\n\n
在上面的输出中,caf\xc3\xa9被编码为63 61 66 c3 a9. 也可以,因为码位U+00E9(对应\xc3\xa9)的UTF-8编码是\\xC3\\xA9(\\x用来表示后面的字符是十六进制数字)。
\\xC3代表 1 个字节, 也是如此\\xA9。因此,UTF-8需要2个字节来表示\xc3\xa9。
PosteRazor从命令的输出复制相同的文本后是:
命令#1:
\n\n\n$ xclip -out -选择剪贴板 -目标 STRING | hexdump -C\n00000000 2f 68 6f 6d 65 2f 67 72 65 65 6e 2f 50 69 63 74 |/home/green/Pict|\n00000010 75 72 65 73 2f 63 61 66 c3 a9 2f |ures/caf../| \n0000001b\n\n\n
显然,Unicode 代码点是混乱的。现在,我们有两个代码点(c3和a9),而应该只有一个(e9)。
毫不奇怪,两个代码点 ieU+00C3和U+00A9代表{LATIN CAPITAL LETTER A WITH TILDE}AND {COPYRIGHT SIGN},这就是我们在 中看到的PosteRazor。
命令#2:
\n\n\n$ xclip -out -选择剪贴板 -目标 UTF8_STRING | hexdump -C\n00000000 2f 68 6f 6d 65 2f 67 72 65 65 6e 2f 50 69 63 74 |/home/green/Pict|\n00000010 75 72 65 73 2f 63 61 66 c3 a9 2f |ures/caf../| \n0000001b\n\n\n
该命令的输出似乎保持不变,但有细微的差别。
\n\n在前面的输出中\\xc3\\xa9形成了单个字符,而现在\\xc3单独形成一个字符并\\xa9形成另一个字符(分别是 \xc3\x83和\xc2\xa9)。
现在我们知道发生了什么,但是它是如何发生的?\n为了模拟同样的事情,我们将使用 Python。我在这里使用Python 3.3.0。
\n\n\n>>> 导入 unicodedata\n>>> a = u\'/home/green/Pictures/caf\xc3\xa9\'\n>>> a\n\'/home/green/Pictures/caf\ xc3\xa9\'\n>>> a = a.encode(\'utf-8\')\n>>> a\nb\'/home/green/Pictures/caf\\xc3\\xa9\' \n>>> a = a.decode(\'iso-8859-15\')\n>>> a\n\'/home/green/Pictures/caf\xc3\x83\xc2\xa9\'\ n>>> a = a.encode(\'utf-8\')\n>>> a\nb\'/home/green/Pictures/caf\\xc3\\x83\\xc2\\xa9\' \n\n\n
您可以看到,如果我们首先使用 UTF-8 对字符串进行编码,然后使用 ISO-8859-15 进行解码,那么我们得到的字符串与使用时得到的字符串相同PosteRazor.
现在,请注意以下代码。在这里,我们也从 nautilus 复制并粘贴了位置:
\n\n\n>>> z = u\'/home/green/Pictures/caf\xc3\xa9\'\n>>> z\n\'/home/green/Pictures/caf\xc3\xa9\'\n >>> z = z.encode(\'iso-8859-15\')\n>>> z\nb\'/home/green/Pictures/caf\\xe9\'\n>>> z = z .decode(\'iso-8859-15\')\n>>> z\n\'/home/green/Pictures/caf\xc3\xa9\'\n\n\n\n
如果我们最初使用 ISO-8859-15 对字符串进行编码,我们就会得到完美的结果。
\n\n请注意,这\\xe9是 ISO-8859-15 中的编码\xc3\xa9,显然需要一个字节。这与 Unicode 代码点 U+00E9 相同,当以 UTF-8 编码时,需要 2 个字节,并表示为\\xc3\\xa9。
现在我们知道一切发生了什么以及如何发生,我们如何纠正它?那么,您可以将路径转换为 ISO-8859-15 字符集,也可以仅使用 GUI 来选择文件。
\n\n来源和更多信息:
\n\n| 归档时间: |
|
| 查看次数: |
490 次 |
| 最近记录: |