Python讀取多個txt檔案並進行儲存

2021-07-22 06:15:02 字數 1477 閱讀 1998

記憶力不好的孩紙得勤做筆記!

最近開始接觸python,現在分享下寫的第二個**,好了,多的不說,直接上**!作業系統為windons.

#coding:utf-8

import matplotlib.pyplot as plt

import numpy as np

#距離temp = 3.0

#定義字典相當於c++中巨集

for i in range(1,16):

path =

#讀第乙個檔案

x =

y =

f = open(path["path1"])

line = f.readline()

while line:

#print line,

a,b = line.split()

line = f.readline()

#print line

f.close()

#string to double

x = [ int( x ) for x in x if x ]

y = [ float( y ) for y in y if y ]

#find the maxdefinition

maxdef = np.max(y)

index = np.where(y == maxdef)

id = x[index[0]]

print maxdef

#print index

print id

#讀另外兩個檔案

u =

v =

w =

f1 = open(path["path2"])

line1 = f1.readline()

while line1:

#print line,

c,d,e = line1.split()

line1 = f1.readline()

#print line

f1.close()

#string to double

u = [ int( u ) for u in u if u ]

v = [ int( v ) for v in v if v ]

w = [ int( w ) for w in w if w ]

#index1 = np.where(w == id)

print u[id]

print v[id]

#write data

f2 = open(path["path3"], 'a')

f2.write(str(u[id]) + ' ' + str(v[id]) + ' ' + str(temp) + '\n')

f2.close()

temp += 0.5

打完收工!!!簡單的實現了對多個文字檔案的讀寫操作~

Python 讀取TXT檔案

一 開啟檔案 f open filename,access mode r buffering 1 filename 檔名 access mode 開啟方式,r讀,w寫,a追加,r w a 都是以讀寫方式開啟,rb二進位制讀,wb二進位制寫,rb wb ab 二進位制讀寫 buffering 預設值 ...

python 讀取txt檔案

txt檔案內容 1.全部讀取 file open e others 測試.txt r 開啟檔案 f all file.read 讀取所有檔案內容 print f all file.close 關閉檔案結果 2.按行讀取 file open e others 測試.txt r 開啟檔案 for lin...

python 讀取txt 檔案

filename users sr00117 desktop bom1.txt txt檔案和當前指令碼在同一目錄下,所以不用寫具體路徑 def readtxt valuelist all list alone list with open filename,r as file to read for...