Python處理 txt 、csv 格式資料
阿新 • • 發佈:2019-02-05
本文做資料處理用,用於去掉資料中大於某個數的數字
data_process.py :處理 txt 格式資料,處理單列
data_process_1.py :處理 csv 格式資料,處理單列
data_process_3.py :處理 csv 格式資料,處理多列
data_process.py :
# coding=utf-8 # 用來處理數字,小於下限和大於上限的數字置為空格 # f = open("/home/chuwei/桌面/trainA/loss/d_losses.txt") f = open("/Users/Administrator/Desktop/logs-35k/loss-35k/g_losses.txt") # f = open("/Users/Administrator/Desktop/logs-160k/loss-160k/g_losses.txt") line = f.readline() #讀取一行 count = 0 count1=0 # print(line) k = open("/Users/Administrator/Desktop/logs-35k/loss-35k/g_losses_1.txt","a") # k = open("/Users/Administrator/Desktop/logs-160k/loss-160k/g_losses_1.txt","a") while line: count=count+1 #計算文字行數 # print(count) if(count>21000): break if((count%1)==0): if (float(count)<10000): if (float(line)<0.1): k.write(" ") k.write("\n") if (float(count)>10000): if (float(line)<1): count1=count1+1 line_prime=line line = 0.0 line = str(line) k.write(" ") # 寫入空格 k.write("\n") # 換行 print("line_prime:",line_prime) print("line:",count,line) print("count1:",count1) elif (float(line)>8): line = 0.0 line = str(line) k.write(" ") k.write("\n") else: k.write(line) else: k.write(line) line = f.readline() # print("line:",(line)) f.close()
data_process_1.py :
# coding=utf-8 # 處理從Tensorboard上下載的資料 # 用來處理CSV檔案,小於下限和大於上限的數字置為空格 import csv # reader函式接收一個可迭代的物件(比如csv檔案),能返回一個生成器。 # 此csv檔案並非二進位制檔案,只是一個文字檔案,'rt'不是'rb' # file="C:/Users/Administrator/Desktop/logs-35k/tensor_data/d_loss.csv" # file="C:/Users/Administrator/Desktop/logs-35k/tensor_data/d_loss_real.csv" # file="C:/Users/Administrator/Desktop/logs-35k/tensor_data/d_loss_fake.csv" file="C:/Users/Administrator/Desktop/logs-35k/tensor_data/g_loss.csv" #開啟檔案,追加a ,寫入模式w,覆蓋掉 out = open('Stu_csv.csv','w', newline='') #設定寫入模式 csv_write = csv.writer(out,dialect='excel') num=0 num1=0 count=1 blank=0 list1=[] stu1 = [26765] print("stu1:",(stu1)) with open(file,'rt') as csvfile: reader=csv.reader(csvfile) rows=[row[2] for row in reader] #遍歷csv的每一行 for row in rows: if (count>750): #750行,對應csv表中 25848 次 break count=count+1 if num>0: #數字 if (float(row)>7.5): row=' ' list1.append([row]) blank=blank+1 else: row=float(row) list1.append([row]) elif num==0: #字元"value" list1.append([row]) num=num+1 for list1_n in list1: #iist 寫入csv的每一行 if(num1==0): for list1_n1 in list1_n: csv_write.writerow(list1_n) else: for list1_n1 in list1_n: # csv_write.writerow([list1_n]) #得到帶有“[]”的數字 csv_write.writerow(list1_n) #得到純數字 print(list1_n1) num1=num1+1 print("blank:",blank) out.close()
處理前:
處理後:去掉第三列中大於7.5的數字
data_process_2.py :
# coding=utf-8 # 處理從Tensorboard上下載的資料 # 用來處理數字,小於下限和大於上限的數字置為空格 import csv # reader函式接收一個可迭代的物件(比如csv檔案),能返回一個生成器。 # 此csv檔案並非二進位制檔案,只是一個文字檔案,'rt'不是'rb' file="C:/Users/Administrator/Desktop/logs-35k/tensor_data/d_loss.csv" #開啟檔案,追加a ,寫入模式w,覆蓋掉 out = open('Stu_csv.csv','w', newline='') #設定寫入模式 csv_write = csv.writer(out,dialect='excel') num=0 space=0 list1=[] list2=[] stu1 = [26765] print("stu1:",(stu1)) with open(file,'rt') as csvfile: reader=csv.reader(csvfile) # rows=[row[2] for row in reader] #遍歷csv的每一行 rows=[row for row in reader] len_num=len(rows) #長度:1001 print(len_num) i=0 while (i<len_num): rows_i = rows[i][2] if num>0: #數字 if (float(rows_i)>1.5): rows_i=' ' list2.append([float(rows[i][0]),float(rows[i][1]),rows_i]) space=space+1 else: list2.append([float(rows[i][0]),float(rows[i][1]),float(rows_i)]) #新增第i行的1、2、3列 elif num==0: #字元"value" list2.append([rows[i][0],rows[i][1],rows_i]) num=num+1 i=i+1 for list_n in list2: #將處理好的資料寫入到CSV文件中 print(list_n[0],list_n[1],list_n[2]) #以數字的形式列印 csv_write.writerow([list_n[0]]+[list_n[1]]+[list_n[2]]) #寫入到三列 print("空格:",space) out.close() # #csv 寫入 # # stu1 = ['marry',26] # #開啟檔案,追加a # out = open('Stu_csv.csv','a', newline='') # #設定寫入模式 # csv_write = csv.writer(out,dialect='excel') # #寫入具體內容 # csv_write.writerow(rows) # csvWriter.writerow([k,v]) # 寫入兩個變數 # out.close()
處理前:
處理後:去掉第三列中大於1.5的數字