Problem Description:
A content stored in the following file.txt
AAAAAA
BBBBBB
CCCCCC
Then using python display, it is found to exhibit such
A A A A A A
B B B B B B
C C C C C C
Why?
Code as follows
Intention is to look for a line (CCC) in the text document, but not always display this line, strange
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子
'''
import io
import os
search_for_this_line = 'CCC'
inf_file = io.open("C://file.txt", mode = 'r+')
lines = inf_file.readlines()
index_temp = 0
for line in lines:
index_temp = index_temp + 1
print line
if search_for_this_line in line:
print "FOUND IT !!!"
break
inf_file.close()
Later found, file.txt fact is unicode utf-16 (16bit for one character) format, and the python default format open to ANSI (single byte for one character), so there will be a question above, we will begin to open line slightly modified, will pass in a manner encoding
inf_file = io.open("C://file.txt", mode = 'r+',encoding = 'utf-16')
You can find it in this line CCC
Also print out
AAAAAA
BBBBBB
CCCCCC
Absolutely correct.