python讀txt檔案讀資料,然後修改資料,再以矩陣形式儲存在檔案中
阿新 • • 發佈:2018-12-11
import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' # -*- coding: UTF-8 -*- import numpy as np import glob import tensorflow as tf flag=True count=0 with open('AVA.txt', 'r') as f, open('1.txt', 'w+') as w: while True: lines = f.readlines() if not lines: break for line in lines: odom = line.split() # id=int(odom[0]) # values = np.array(odom[0:15], dtype='int') values=list(map(int,odom)) if values[0]==2234 or values[0]==10632 or values[0]==23848 or values[0] ==36266 or values[0]==57942 or values[0]==176140: flag = False count += 1 print("lines", line) else: if flag == True: # b = str(myArray) w.writelines(line) else: values[0] -=count # b = str(myArray) w.writelines(str(values).replace(',','').replace('[','').replace(']','')) #去掉逗號和方括號 w.write('\n')
用replace(src, des)#des為需要替換成的目標字串,src為需要替換的源字串