Python读写大文件-将整行文字根据分号分行
f = open("DDD_sales.txt", 'r', True) filepath="data1.txt" f1 = open(filepath, mode="a", encoding="utf-8") line="" ix=0 while True: # 每次读取一个字符 ch = f.read(1) # 如果没有读到数据,跳出循环 if not ch: break line= line+str(ch) if(ch==";"): ix=ix+1 f1.write(str(line[0:len(line) - 1])+ "\n") f1.flush() print(str(ix)) line="" f1.close() f.close()