chr(10)、" "、"\n"、r" " 你搞清楚了吗?

一些非常恐怖的接口不但不传json,还搞些奇奇怪怪的分隔符,比如:

每条记录用 Chr(10)分隔,每列用
 分隔

chr(10)不就是换行符" "吗?这两者有什么区别?

实际拿到数据,print出来是这样的:

Avalue1
Avalue2
Avalue3
Bvalue1
Bvalue2
Bvalue3

在print()函数中,chr(10)会变成换行符,而r" "会变成

$ a = f"this\nis{chr(10)}a
test\nstring"
$ print(a)
this
is
a
test
string

因此,这个接口是用换行符来分割每条记录的,但是每列是用" "这两个字符来分割的,在python里面应写作r" "或者"\n"(忽略转义)

这两者是不同的东西

总结:chr(10)与" "是同一种东西,"\n"r" "是同一种东西

>>> chr(10)=="
"
True
>>> "\n"=="
"
False
>>> "\n"==r"
"
True
a = f"this\nis{chr(10)}a
test\nstring"
print(a.split(chr(10)))
# ['this\nis', 'a', 'test\nstring']
print(a.split('
'))
# ['this\nis', 'a', 'test\nstring']
print(a.split('\n'))
# ['this', 'is
a
test', 'string']
print(a.split(r'
'))
# ['this', 'is
a
test', 'string']
原文地址:https://www.cnblogs.com/luozx207/p/13156129.html