如何打印出字符串“\b”在Python [英] How can I print out the string "\b" in Python

查看:3312
本文介绍了如何打印出字符串“\b”在Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的编译器类中,我决定用Python编写我的编译器,因为我喜欢Python编程,虽然我遇到一个有关字符打印的有趣的问题。我写的词法需要包含formfeed和backspace字符的字符串以非常特殊的方式打印到stdout:用双引号括起来,分别打印为\f和\b。最接近的我已经得到:

In my compilers class, I decided to write my compiler in Python since I enjoy programming in Python, though I encountering an interesting issue with how characters are printed. The lexer I'm writing requires that strings containing the formfeed and backspace characters be printed to stdout in a very particular way: enclosed in double quotes, and printed as \f and \b, respectively. The closest I've gotten:

print("{0!r}".format("\b\f"))

产生

'\x08\x0c'

请注意单引号,和utf8编码。与我关心的两个其他字符相同的命令几乎可以工作:

Note the single quotes, and utf8 coding. The same command with two other characters I'm concerned with almost works:

print("{0!r}".format("\n\t"))

提供:

'\n\t'

要清楚,我需要符合规范的结果(包括引号)是

To be clear, the result (including quotes) that I need to conform to the spec is

"\b\f"

找到\b和\f等简单方法, \\ b和\f似乎不工作...\只是Python打印反斜杠的方式,所以我似乎永远不会只是\b \f作为一个

Simple approaches like finding \b and \f and replacing them with "\b" and "\f" don't seem to work...the "\" is just the way Python prints a backslash, so I can never seem to get just "\b\f" as one might expect.

使用各种字符串编码似乎没有帮助。我得出结论,我需要写一个自定义string.Formatter,但我想知道是否有另一种方法,我会错过。

Playing with various string encodings doesn't seem to help. I've concluded that I need to write a custom string.Formatter, but I was wondering if there is another approach that I'd missed.

编辑:谢谢所有答案。我不认为我做了一个工作问这个问题。基本的问题是,我将字符串格式化为原始,因为我想要文字换行符显示为\\\
和文字选项卡显示为\t。但是,当我使用原始格式化打印字符串时,我失去打印\b和\f的能力,因为下面的所有答案建议。

Thanks for all the answers. I don't think I did that good of a job asking the question though. The underlying issue is that I'm formatting the strings as raw because I want literal newlines to appear as "\n" and literal tabs to appear as "\t". However, when I move to print the string using raw formatting, I lose the ability to print out "\b" and "\f" as all the answers below suggest.

今晚我会确认,但根据这些答案,我认为我应该使用的方法是正常格式化输出,并捕获所有字面值\\\
,\t, \\ b和\f字符以及将根据需要打印的转义序列。我仍然希望避免使用string.Formatter。

I'll confirm this tonight, but based on these answers, I think the approach I should be using is to format the output normally, and trap all the literal "\n", "\t", "\b", and "\f" characters with escape sequences that will print them as needed. I'm still hoping to avoid using string.Formatter.

EDIT2:我要使用的最后一种方法是使用非原始字符串格式。非抽象版本如下:

The final approach I'm going to use is to use non-raw string formatting. The non-abstracted version looks something like:

print('"{0!s}"'.format(a.replace("\b", "\\b").replace("\t", "\\t").replace("\f", "\\f").replace("\n","\\n")))


推荐答案

print("{0!r}".format("\b\f".replace("\b", "\\b").replace("\f", "\\f")))

或者更干净:

def escape_bs_and_ff(s):
    return s.replace("\b", "\\b").replace("\f", "\\f")

print("{0!r}".format(escape_bs_and_ff("\b\f"))

这篇关于如何打印出字符串“\b”在Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆