首页 > 解决方案 > Python:处理大字符串值 - if 语句无法正常工作?

问题描述

我有这个大字符串值,大约 500 个字符,称为 'strPage6' 我正在寻找这个字符串的第 n 个位置是否有一个 if 语句没有成功的 '%' 值。

#see if 330th character has value '%'
if (strPage6[330:331]=="%"):
   print("330th character is '%' ")
   print(strPage6)
else:
   print("330th character is NOT '%' ")
   print(strPage6)

我的终端返回以下值:

330th character is NOT '%'
b'%'

我尝试将字节字符串转换为带有“解码”的纯字符串,结果如下:

strPage6=strPage.decode('utf-8')
[...]

330th character is NOT '%'
e

基本上,我希望得到以下结果:

330th character is '%'
%

感谢您的帮助!

标签: pythonstringif-statementbyte

解决方案


您是否尝试过 strPage6[330]==b"%"?– 凯尔伍德

等式是否正确


推荐阅读