- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这段代码有问题:
import optparse
parser = optparse.OptionParser(version=__version__,
usage="%prog [options] file1 ... host[:dest]",
description=main.__doc__)
parser.add_option("-c", "--config", help="Specify an alternate config "
"file. Default = '%s'" % config_file)
parser.add_option('-l', '--log-level', type="choice",
choices=LOG_LEVELS.keys(),
help="Override the default logging level. Choices=%s, Default=%s" %
(",".join(LOG_LEVELS.keys()), LOG_LEVEL))
parser.add_option("-o", "--overwrite", action="store_true",
help="If specified, overwrite existing files at destination. If "
"not specified, throw an exception if you try to overwrite a file")
parser.add_option('-s', "--speed", action="store_true", \
help="If specifed, print the data transfer rate for each file "
"that is uploaded (infers verbose option)")
parser.add_option('-v', '--verbose', action="store_true",
help="If specified, print every file that is being uploaded and every "
"directory that is being created")
parser.add_option("-u", "--user", help="The username to use for "
"authentication. Not needed if you have set up a config file.")
parser.add_option("-p", "--password", help="The password to use for "
"authentication. Not needed if you have set up a config file.")
parser.set_defaults(config=config_file, log_level=LOG_LEVEL)
options, args = parser.parse_args()
print (args)
如您所见,当我打印我们正在使用希伯来语命名文件进行的测试的参数时,打印结果包括:['/root/mezeo_sdk/1/\xfa\xe5\xeb\xf0\xe9\xfa\xf2\xe1\xe5\xe3\xe4.xlsx', '主机名']而不是/root/mezeo_sdk/1/"תוכנית עבודה.xlsx"
此外,脚本将文件上传到服务器后的最终结果(文件名的传递方式)是:http://i.imgur.com/pP6fA.png
文件名本身在 linux 源上很好,因为如果我将它 SCP 到我自己的计算机上它看起来不错,但一旦我使用 python 脚本将它传输到文件服务器后就不行了。
我也不认为问题出在文件服务器端,因为如果我使用网络界面上传希伯来文命名的文件,它们就可以了。
我认为问题在于 optparse 库的使用。
最佳答案
一如既往,我将从 Unicode 建议阅读开始:您真的应该阅读其中一个或两个
简而言之,Unicode 代码点是代表一个字符1 的抽象“东西”。程序员喜欢使用这些,因为我们喜欢将字符串视为一次一个字符。不幸的是,很久以前就规定一个字符必须适合一个字节的内存,因此最多可以有 256 个不同的字符。这对简单的英语很好,但对其他任何东西都不起作用。有一个全局代码点列表——数以千计——旨在包含每个可能的字符,但显然它们不适合一个字节。
解决方案:构成字符串的代码点的有序列表与其作为字节序列的编码之间存在差异。每当你使用一个字符串时,你必须清楚它应该是这些形式中的哪一种。要在形式之间转换,你可以 .encode()
代码点列表(Unicode 字符串)作为字节列表,并将 .decode()
字节转换为代码点列表。为此,您需要知道如何将代码点映射到字节,反之亦然,这就是编码。
1有点。
好吧,现在已经不碍事了,让我们看看你有什么。您已经给出了一个(原始)字符串——一个字节序列:
\xfa\xe5\xeb\xf0\xe9\xfa \xf2\xe1\xe5\xe3\xe4
你想成为的编码
תוכנית עבודה
一点谷歌搜索告诉我你正在使用 Windows-1255编码,它是 ASCII 的扩展,使用高字节来保存希伯来字母。您希望使用 Unicode 格式的字符串,因为 Unicode 表示普通数据。因此,您应该使用编码"Windows-1255"
解码
字节序列:
>>> s
'\xfa\xe5\xeb\xf0\xe9\xfa \xf2\xe1\xe5\xe3\xe4'
>>> s.decode("Windows-1255")
u'\u05ea\u05d5\u05db\u05e0\u05d9\u05ea \u05e2\u05d1\u05d5\u05d3\u05d4'
现在您拥有正确分类的数据。接下来,您需要将数据发送到服务器,这意味着将其编码为正常编码,即“UTF-8”:
>>> s.decode("Windows-1255").encode("utf-8")
'\xd7\xaa\xd7\x95\xd7\x9b\xd7\xa0\xd7\x99\xd7\xaa \xd7\xa2\xd7\x91\xd7\x95\xd7\x93\xd7\x94'
最后,你可能想知道服务器哪里出了问题。好吧,如果您不指定数据编码,人们将不得不猜测,这是一个注定要失败的企业。在您的情况下,您似乎将原始字节发送到服务器,然后服务器将它们解码为 latin-1
。这给出了您看到的奇怪的重音字母,因为 latin-1 使用 ASCII 字节的上半部分,而不是希伯来语字符,而是用于重音英语字符。
故事的寓意:理解 Unicode!
关于Python 文件名/路径解析错误的希伯来语编码(使用 optparse 库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10223322/
我正在尝试使用 optparse-applicative程序中的库应根据参数的数量执行不同的操作。 例如,计算周长的程序的参数解析: module TestOpts where import Opti
./hello_world -c arg1 arg2 arg3 是否可以通过编码使选项 -c 仅获得两个参数(arg1 和 arg2)? parser.add_option("-c",
我正在使用 optparse,最近遇到以下问题 - 我想将目录名称作为参数传递。像这样的东西: ./script.py --dir c:\a\b 但是,optparse 消除了“\”符号,因此相关变量
我有一个 mysql 数据库,我正在尝试打印特定学生的所有测试结果。我正在尝试创建一个命令行,在其中输入用户名,然后它将显示他/她的测试结果。我已经访问过此页面,但无法得到答案。 optparse a
通常,可以不带任何参数地调用 optparse 的方法 parse_args。但是,如果需要提供与 sys.argv 不同的参数集,则可以传递给 parse_args。 但是,如果需要将字符串而不是列
我正在开发一个接收一些参数并希望其中一些参数成为必需参数的程序,但我遇到了一些问题: 我确实需要使代码与 Python 2.4.x 兼容,所以我(至少是这么认为)只能使用 optparse 希望避免代
有没有办法让下面的工作?我正在寻找的是根据另一个选项的值获得一个选项的值。 import optparse parser = optparse.OptionParser() parser.add_op
请说明为什么下面的代码给出了错误,尽管两者都表示相同的选项。 In [3]: parser = optparse.OptionParser() In [4]: parser.add_option("-
我是 Python 的新手,并且在玩 optparse。我有这样的解析器功能: def parse(argv): """Option parser, returns the options l
我正在尝试使用 optparse,但遇到问题。 我的脚本用法是:script 我不打算添加任何选项字符串,例如:script -f 或script --file 有什么方法可以选择不传递参数字符串
我编写了一个最多接受 4 个选项和 2 个参数的函数。选项包括 -1、-2、-3 和 -u。默认情况下,它们的值分别设置为 true、true、true 和 false,但启用任何选项都会导致该值翻转
我想将数据传递给脚本,就像这样 if __name__ == '__main__': usage = 'python pull.py [-h ][-p ][-r ]arg1[,arg2..]'
if __name__=='__main__': parser = OptionParser() parser.add_option("-i", "--input_file",
我遇到了以下有趣的错误: parser.add_option("-n", "--number", metavar="NUMBER", type="int", hel
我目前正在学习如何使用 Python optparse 模块。我正在尝试以下示例脚本,但 args 变量结果为空。我使用 Python 2.5 和 2.6 进行了尝试,但无济于事。 import op
我试图更好地了解 optparse,但我很难理解为什么以下代码会这样运行。我在做傻事吗? import optparse def store_test(option, opt_str, value,
我正在编写一个 python 脚本,我希望它能够从命令行调用并作为库函数导入。理想情况下,命令行选项和函数应使用同一组默认值。允许我在两个地方重复使用一组默认值的最佳方法是什么? 这是具有重复默认值的
采用以下相当标准的代码: from optparse import OptionParser opts = OptionParser() opts.ad
当使用 optparse 时,我想在一个选项之后获取整个字符串,但我只获取到第一个空格的一部分。 例如: python myprog.py --executable python someOtherP
是否有 optparse (command line option parser) 的 C# 端口?来自 Python 的模块在一些 OSI-approved license 下可用? 最佳答案 你看
我是一名优秀的程序员,十分优秀!