小编f0r*_*d42的帖子

基于argparse的调用函数

我是python的新手,现在正在使用它.我有一个脚本,它可以对设备执行一些API调用.我想扩展功能,并根据调用脚本时给出的参数调用不同的函数.

目前我有以下内容:

parser = argparse.ArgumentParser()
parser.add_argument("--showtop20", help="list top 20 by app",
                    action="store_true")
parser.add_argument("--listapps", help="list all available apps",
                    action="store_true")
args = parser.parse_args()
Run Code Online (Sandbox Code Playgroud)

我也有

def showtop20():
    .....
Run Code Online (Sandbox Code Playgroud)

def listapps():
....
Run Code Online (Sandbox Code Playgroud)

如何根据给出的参数调用函数(并且只调用此函数)?我不想跑

if args.showtop20:
   #code here

if args.listapps:
   #code here
Run Code Online (Sandbox Code Playgroud)

因为我想稍后将不同的功能移动到模块上,以保持主要的可执行文件干净整洁.

python argparse

28
推荐指数
4
解决办法
4万
查看次数

Python pandas:删除字符串中分隔符后的所有内容

我有数据框,包含例如:

"vendor a::ProductA"
"vendor b::ProductA
"vendor a::Productb"
Run Code Online (Sandbox Code Playgroud)

我需要删除所有(包括)两个::所以我最终得到:

"vendor a"
"vendor b"
"vendor a"
Run Code Online (Sandbox Code Playgroud)

我试过str.trim(似乎不存在)和str.split没有成功.最简单的方法是什么?

python python-3.x pandas

14
推荐指数
2
解决办法
3万
查看次数

python - dateutil / 解析器中的语言环境

我设置

locale.setlocale(locale.LC_TIME, ('de', 'UTF-8'))
Run Code Online (Sandbox Code Playgroud)

要解析的字符串是:

Montag, 11. April 2016 19:35:57
Run Code Online (Sandbox Code Playgroud)

我用:

note_date = parser.parse(result.group(2))
Run Code Online (Sandbox Code Playgroud)

但得到以下错误:

回溯(最近一次调用):文件“/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py”,第 1531 行,在 globals = debugger.run(setup['file'], None, None, is_module ) File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 938, in run pydev_imports.execfile(file, globals, locals) # 执行脚本 File "/Applications/PyCharm.app/Contents /helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/Users/adieball/Dropbox/Multiverse/Programming /python/repositories/kindle/kindle2en.py", line 250, in main(sys.argv[1:]) 文件 "/Users/adieball/Dropbox/Multiverse/Programming/python/repositories/kindle/kindle2en.py",第 154 行,在主要 note_date = parser.parse(result.group(2)) 文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/dateutil/parser.py”,第 1164 行,在解析中返回 DEFAULTPARSER.parse(timestr, **kwargs) 文件“/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/dateutil/parser.py”,第 555 行,在解析中raise ValueError("Unknown string format") ValueError: Unknown string format在解析引发 ValueError("Unknown string format") …

python python-dateutil

6
推荐指数
1
解决办法
1793
查看次数

PowerQuery - 检查列值是否等于 true

我根据我的源数据在 PowerQuery 中构建了几个附加列。这包括两个仅返回“TRUE”或“FALSE”的“TextContains”列。我现在想要一个额外的列来突出显示不同的服务类型并使用此:

if [PSTag] = "PS" then "PS" 
else if [Trainingskit] = "TrainingsKit" then "Training"
else if [Training] = "Training" then "Training"
else if [HardwareService] = "TRUE" then "HardwareService"
else if [TelephoneService] = "TRUE" then "TelephoneService" else "NonService"
Run Code Online (Sandbox Code Playgroud)

它对于前三个 IF 语句运行良好,但对于仅包含“TRUE”或“FALSE”的列根本不起作用。前三个包含“PS”或“NonPS”或“Training”或“NonTraining”

我确信我“只是”错过了一个非常基本的东西。

非常感谢任何帮助。

powerquery

3
推荐指数
1
解决办法
3万
查看次数

Python:如何解析"返回"字符串

我正在编写一个通过导入使用现有框架的小工具.

我正在使用的部分是:

def exec_command(self, command, timeout=60, mode=cli.CLIMode.UNDEF,
                     output_expected=None, error_expected=False, prompt=None):
        """Executes the given command.

        This method handles detecting simple boolean conditions such as
        the presence of output or errors.

        :param command:  command to execute, newline appended automatically
        :param timeout:  maximum time, in seconds, to wait for the command to
            finish. 0 to wait forever.
        :param mode:  mode to enter before running the command. The default
            is :func:`default_mode`.  To skip this step and execute directly
            in the cli's current mode, explicitly …
Run Code Online (Sandbox Code Playgroud)

python parsing

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

使用 .encode 使用 utf8 读取行

我从文件中读取行,例如:

\n\n
\n

小大事:163 Wege zur Spitzenleistung (Dein Leben)(德语版)(彼得斯、汤姆)

\n\n

Die madlle Katastrophe: So f\xc3\xbchren Sie Teams \xc3\xbcber Distanz zur\n Spitzenleistung (德语版) (Thomas, Gary)

\n
\n\n

我用以下方法读取/编码它们:

\n\n
title = line.encode(\'utf8\')\n
Run Code Online (Sandbox Code Playgroud)\n\n

但输出是:

\n\n
\n

b\'Die 美德灾难:所以 f\\xc3\\xbchren Sie Teams \\xc3\\xbcber\n Distanz zur Spitzenleistung(德语版)(托马斯、加里)\'

\n\n

b\'The Little Big Things: 163 Wege zur Spitzenleistung (Dein Leben)\n(德语版)(Peters,Tom)\'

\n
\n\n

为什么总是添加“b\'”?\n如何正确读取文件以便保留“元音变音”?

\n\n

这是完整的相关代码片段:

\n\n
# Parse the clippings.txt file\nlines = [line.strip() for line in codecs.open(config[\'CLIPPINGS_FILE\'], \'r\', \'utf-8-sig\')]\nfor line in lines:\n    line_count = line_count …
Run Code Online (Sandbox Code Playgroud)

python unicode utf-8

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