def long_to_2(str_):
for i in range(0,len(str_)+1,2):
print(str_[i:i+2])
字符串分割后,常转换为十六进制输出。
一般来说,先换算为十进制,再进行后续转换
file = open("sample.txt")
for line in file:
pass # do something
file = open("sample.txt")
while 1:
lines = file.readlines(100000)
if not lines:
break
for line in lines:
pass # do something
import fileinput
for line in fileinput.input("sample.txt"):
pass
file = open("sample.txt")
while 1:
line = file.readline()
if not line:
break
pass # do something
其中,后两种方法都不推荐。
参考文章 https://blog.0kami.cn/2016/09/16/old-python-sandbox-escape/
另外补充:当getattr()方法被禁后,可以使用‘getattribute’属性。
import os
rootdir = os.getcwd()#可自定义
for parent, dirnames, filenames in os.walk(rootdir):
for file in filenames:
with open(parent +'\\' + file, 'r') as f:
text = f.read() #print(text[0:5])
if text[0:11111] == '待比较字符串'
print(text)