相关疑难解决方法(0)

字符串格式选项:优点和缺点

这是在Python中格式化字符串的两种非常流行的方法.一个是使用dict:

>>> 'I will be %(years)i on %(month)s %(day)i' % {'years': 21, 'month': 'January', 'day': 23}
'I will be 21 on January 23'
Run Code Online (Sandbox Code Playgroud)

而另一个使用简单tuple:

>>> 'I will be %i on %s %i' % (21, 'January', 23)
'I will be 21 on January 23'
Run Code Online (Sandbox Code Playgroud)

第一个是更具可读性,但第二个更快写.我实际上模糊地使用它们.

每个人的利弊是什么?关于性能,可读性,代码优化(其中一个转换为另一个?)以及您认为有用的任何其他内容.

python string-formatting

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

格式化控制台输出

我无法正确对齐python打印文本.我已经尝试过我所知道的一切,但结果仍然相同而且非常烦人!

这是我在控制台中获得的内容 在此输入图像描述

这是我的代码.

print " FileName\t\t\t\t\tStatus\t\tBinary Type\n"  

for files in PASS:
    log = subprocess.check_output(['dumpbin','/HEADERS',files])
    if arch64 in log:
        print" %s \t\t\t\tPASSED\t\t 64-bit \t\t  " %files 
    elif arch32 in log:
        print" %s \t\t\t\tPASSED\t\t 32-bit \t\t  " %files
print"\n"   
for files in FAILED:

    print" %s \t\t\t\t  FAILED \t\t        " %files

print "\n\n
Run Code Online (Sandbox Code Playgroud)

python text-formatting console-output

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

python TypeError没有足够的格式字符串参数

试图用python fabric更新文件

run('sed -i s/jahil/%s/g /etc/nginx/sites-enabled/%s' % (account))
Run Code Online (Sandbox Code Playgroud)

要么

run('sed -i "s/jahil/%s/g" /etc/nginx/sites-enabled/%s' % (account))
Run Code Online (Sandbox Code Playgroud)

错误:

TypeError: not enough arguments for format string
Run Code Online (Sandbox Code Playgroud)

任何的想法 ?非常感谢 !

python regex sed fabric

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