我是靠谱客的博主 呆萌身影,最近开发中收集的这篇文章主要介绍python invalid_为什么'\x'在Python中无效?,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I was experimenting with '' characters, using 'abc...' just to enumerate for myself which characters Python interprets as control characters, and to what. Here's what I found:

a - BELL

b - BACKSPACE

f - FORMFEED

n - LINEFEED

r - RETURN

t - TAB

v - VERTICAL TAB

Most of the other characters I tried, 'g', 's', etc. just evaluate to the 2-character string of a backslash and the given character. I understand this is intentional, and makes sense to me.

But 'x' is a problem. When my script reaches this source line:

val = "x"

I get:

ValueError: invalid x escape

What is so special about 'x'? Why is it treated differently from the other non-escaped characters?

解决方案

There is a table listing all the escape codes and their meanings in the documentation.

Escape Sequence Meaning Notes

xhh Character with hex value hh (4,5)

Notes:

4. Unlike in Standard C, exactly two hex digits are required.

5. In a string literal, hexadecimal and octal escapes denote the byte

with the given value; it is not necessary that the byte encodes a character

in the source character set. In a Unicode literal, these escapes denote a

Unicode character with the given value.

最后

以上就是呆萌身影为你收集整理的python invalid_为什么'\x'在Python中无效?的全部内容,希望文章能够帮你解决python invalid_为什么'\x'在Python中无效?所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部