python 文件查找及截取字符串 (替换,分割) demo
#"F:\\test.txt" ''' # 例1:字符串截取 str = '12345678' print str[0:1] # 例2:字符串替换 str = 'akakak' str = str.replace('k',' 8') # 将字符串里的k全部替换为8 # 例3:字符串查找 str = 'a,hello' print str.find('hello') # 在字符串str里查找字符串hello >> 2 # 输出结果 例4:字符分割 str = 'a,b,c,d' strlist = str.split(',') # 用逗号分割str字符串,并保存到列表 for value in strlist: # 循环输出列表值 print value >> a # 输出结果 >> b >> c >> d ''' data = '' with open('F:\\test.txt', 'r') as f: for line in f.readlines(): line = line.strip() #print(line) idx = line.find("orderNo") if(idx != -1): print(line[idx+9:idx+26]) data += line