小编Sta*_*nch的帖子

为什么Python在语言环境模块中将德语和西班牙语(以及其他?)时间格式字符串存储为%T?

我正在为将在多个语言环境中使用的程序编写测试.在用德语运行测试时,我收到了错误

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/_strptime.py", line 454, in _strptime_time
    return _strptime(data_string, format)[0]
  File "/usr/local/lib/python2.7/_strptime.py", line 317, in _strptime
    (bad_directive, format))
ValueError: 'T' is a bad directive in format '%T'
Run Code Online (Sandbox Code Playgroud)

深入研究,我发现使用locale.nl_langinfo(locale.T_FMT)德语或西班牙语(以及可能的其他语言)生成格式字符串'%T'.这在time模块中无法识别.

对文档localepython.org没有有关返回提什么'%T'.'%T'我可以在任何地方找到的唯一引用是对单独的StackOverflow问题的响应.从那篇文章和背景来看,我假设'%T'是简写'%H:%M:%S'.

我的问题是,我如何处理locale将返回'%T'其格式字符串的语言环境,而不做类似的事情

if fmt_str == '%T':
    fmt_str = '%H:%M:%S'
Run Code Online (Sandbox Code Playgroud)

处理这些案件?

python time locale string-formatting

8
推荐指数
1
解决办法
404
查看次数

如何在我的.py文件中成功使用UNICODE字符而不会造成麻烦?

我正在为一个包含瑞典字符的数据库编写测试.在测试中,我直接使用带有变音符号和其他此类瑞典语连字的字符,它运行得很好,从数据库中读取文件名并成功进行字符串比较.

但是,在导入此文件以执行pydoc生成时,我得到了一个非常熟悉的异常:

SyntaxError:第1行文件foo.py中的非ASCII字符'\ xc3',但未声明编码; 有关详细信息,请参阅http://www.python.org/peps/pep-0263.html

在我自己做一些调查后,我发现添加了

# -*- coding: iso-8859-15 -*-
Run Code Online (Sandbox Code Playgroud)

在我的文件的顶部修复了导入问题.但是,现在测试失败了所有的字符串比较.我尝试了另一种方法来放弃编码声明并将字符串写为

u"Bokmärken"
Run Code Online (Sandbox Code Playgroud)

......但这仍然无法使测试失败.

有谁知道解决这个问题的好方法?

python unicode

4
推荐指数
1
解决办法
3602
查看次数

Argparse 无法识别唯一的位置参数

Argparse 不承认我的位置论点。这是设置:

parser = argparse.ArgumentParser()
parser.add_argument("url")
parser.add_argument("-u", "--username")
parser.add_argument("-p", "--password")
parser.add_argument("-d", "--depth", default=sys.maxsize)
parser.add_argument("-t", "--threads", default=4)
Run Code Online (Sandbox Code Playgroud)

在底部,我有这个:

if __name__ == "__main__":
    if len(sys.argv) > 1:
        print sys.argv
        args = parser.parse_args(sys.argv)
        if args.username is not None and args.password is not None:
            auth = [args.username, args.password]
        else:
            auth = None

        spider(args.url,
               basic_auth=auth,
               threads=int(args.threads),
               depth=int(args.depth))
    else:
        parser.print_help()  # Handle 0 command-line arguments
Run Code Online (Sandbox Code Playgroud)

当我在命令行上调用我的脚本时staunch$ ./spiderer.py http://www.example.com/,我得到了这个:

usage: spiderer.py [-h] [-u USERNAME] [-p PASSWORD] [-d DEPTH] [-t THREADS]
                   url
spiderer.py: error: unrecognized arguments: …
Run Code Online (Sandbox Code Playgroud)

python argparse

2
推荐指数
1
解决办法
1456
查看次数

标签 统计

python ×3

argparse ×1

locale ×1

string-formatting ×1

time ×1

unicode ×1