简体   繁体   English

外壳文本到python字符串

[英]Shell text to python string

I'm writing a little python utility to help move our shell -help documentation to searchable webpages, but I hit a weird block : 我正在写一个小蟒蛇工具,以帮助推动我们的壳-help文档搜索的网页,但我打了一个奇怪的块:

output = subprocess.Popen([sys.argv[1], '--help'],stdout=subprocess.PIPE).communicate()[0]
output = output.split('\n')
print output[4]
#NAME
for l in output[4]:
    print l
#N
#A
#
#A
#M
#
#M
#E
#
#E

#or when written, n?na?am?me?e

It does this for any heading/subheading in the documentation, which makes it near unusable. 它将对文档中的任何heading/subheading执行此操作,从而使其几乎无法使用。

Any tips on getting correct formatting? 关于正确格式的任何提示? Where did I screw up? 我在哪里弄糟?

Thanks 谢谢

The documentation contains overstruck characters done in the ancient line-printer way: print each character, followed by a backspace ( \\b aka \\x08 ), followed by the same character again. 该文档包含以古老的行式打印机方式完成的过划线字符:打印每个字符,后跟退格键( \\b aka \\x08 ),然后再次输出相同字符。 So "NAME" becomes "N\\bNA\\bAM\\bME\\bE" . 因此,“ NAME”变为"N\\bNA\\bAM\\bME\\bE" If you can convince the program not to output that way, it would be the best; 如果您可以说服程序不要以这种方式输出,那将是最好的选择。 otherwise, you can clean it up with something like output = re.sub(r'\\x08.', '', output) 否则,您可以使用output = re.sub(r'\\x08.', '', output)清理它

A common way to mark a character as bold in a terminal is to print the character, followed by a backspace characters, followed by the character itself again (just like you would do it on a mechanical typewriter). 在终端中将字符标记为粗体的一种常见方法是先打印字符,再打印退格字符,再打印字符本身(就像在机械打字机上一样)。 Terminal emulators like xterm detect such sequences and turn them into bold characters. 诸如xterm类的终端仿真器会检测此类序列并将其转换为粗体字符。 Programs shouldn't be printing such sequences if stdout is not a terminal, but if your tool does, you will have to clean up the mess yourself. 如果stdout不是终端,程序不应该打印这样的序列,但是如果您的工具是,则您必须自己清理混乱。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM