我正在尝试使用以下代码进行字符串匹配,但我遇到了TypeError:Type str不支持缓冲区API。怎么解决这个问题?
file = open("C:\\Users\\sagars\\Desktop\\newips.json", "rb")
i = 0
y = "No information available for that IP"
for line in file:
z = line
print (z)
if y in z:
print ("Cannot parse")
else:
print ("Can Parse!")
答案 0 :(得分:0)
您正在将文件读取为二进制文件('rb'而不是'r'),这可能会弄乱它。这段代码更加惯用:
with open('C:\\Users\\sagars\\Desktop\\newips.json', 'r') as f:
line = f.readline()
if y in line:
print ("Cannot parse")
else:
print ("Can Parse!")