简体   繁体   English

如何在Python中打印出字符串“\\ b”

[英]How can I print out the string “\b” in Python

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. 在我的编译器类中,我决定用Python编写我的编译器,因为我喜欢用Python编程,尽管我遇到了一个关于如何打印字符的有趣问题。 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. 我正在编写的词法分析器要求包含formfeed和backspace字符的字符串以非常特殊的方式打印到stdout:用双引号括起来,分别打印为\\ f和\\ b。 The closest I've gotten: 我得到的最接近的:

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

which yields 产量

'\x08\x0c'

Note the single quotes, and utf8 coding. 注意单引号和utf8编码。 The same command with two other characters I'm concerned with almost works: 与我关注的另外两个角色相同的命令几乎可以工作:

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

gives: 得到:

'\n\t'

To be clear, the result (including quotes) that I need to conform to the spec is 要清楚,我需要符合规范的结果(包括引号)是

"\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. 像查找\\ b和\\ f以及用“\\ b”和“\\ f”替换它们这样的简单方法似乎不起作用......“\\”只是Python打印反斜杠的方式,所以我似乎永远不会正如人们所期望的那样得到“\\ b \\ f”。

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. 我已经得出结论,我需要编写一个自定义字符串.Formatter,但我想知道是否还有其他方法我错过了。

EDIT: 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". 根本问题是我将字符串格式化为原始字符串,因为我希望文字换行符显示为“\\ n”,文字选项卡显示为“\\ 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. 但是,当我使用原始格式打印字符串时,我无法打印出“\\ b”和“\\ f”,如下面的所有答案所示。

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. 我今晚会证实这一点,但基于这些答案,我认为我应该使用的方法是正常格式化输出,并捕获所有文字“\\ n”,“\\ t”,“\\ b”和“ \\ f“具有转义序列的字符,将根据需要打印它们。 I'm still hoping to avoid using string.Formatter. 我仍然希望避免使用string.Formatter。

EDIT2: The final approach I'm going to use is to use non-raw string formatting. EDIT2:我将要使用的最后一种方法是使用非原始字符串格式。 The non-abstracted version looks something like: 非抽象版本看起来像:

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

Use raw string: 使用原始字符串:

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

Or, more cleanly: 或者,更干净:

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

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

The r indicates a raw or verbatim string, which means that instead of trying to parse things like \\n into a newline, it literally makes the string \\n . r表示原始字符串或逐字符串字符串,这意味着它不是尝试将类似\\n解析为换行符,而是字面上使字符串为\\n

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

相关问题 如何在 Python 中将字符串“3x a;2x b”转换为字符串“aaabb”? - How can I convert string like '3x a;2x b' to string 'a a a b b' in Python? python:我如何比较 doc A 和 doc B,如果它在 doc B 中找到匹配的字符串,我该如何打印整行? - python: How do I compare doc A and doc B and if it finds a string that matches in doc B how do I print the entire line? python:如何打印我的文件行? - python:How can I print out my file lines? 如何使用 python 在 JSON 中打印出多个 object? - How can I print out multiple object in JSON using python? 如何将推文复制到 python 并打印出来? - How can I copy a tweet into python and print it out? MySql的Python脚本在0x7f79ce81b440>行中打印出SRE_Match对象,如何获取它以打印实际的查询结果? - Python script for MySql prints out SRE_Match object at 0x7f79ce81b440> lines, how can I get it to print actual query results? 如何在Python中将0x1b87打印为\\ x1b \\ x87? - How can I get 0x1b87 to print like \x1b\x87 in Python? 如何在python中找到两个字符串之间的字符串并打印出来? - How can I find and print a string between two strings in python? 如何在Linux上使用Python解析JSON字符串并打印出来? - How can I parse JSON string and print it using Python on linux? 如何在YAML文件的Python中打印不带字符串的[] - How can I print [] without string in Python in YAML file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM