在 for 循环中附加一个临时文件
Appending a temp file in a for loop
我有三个数据数组。如果满足某些条件,我想循环它们并将值保存在临时文件中。如果条件不满足我想打开临时文件并找到最大值的索引然后保存到另一个文件。当我尝试下面的代码时,出现此错误。这是我第一次使用 tempfile.NamedTemporaryFile()
,所以我很可能没有正确使用它。谢谢
Traceback (most recent call last):
File "<ipython-input-19-7c44ca7dcbd6>", line 1, in <module>
runfile('C:/Users/Khary/Documents/Astrophysics/Bolshoi/Halo Formation History Project/Codes/Find V_max.py', wdir='C:/Users/Khary/Documents/Astrophysics/Bolshoi/Halo Formation History Project/Codes')
File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/Khary/Documents/Astrophysics/Bolshoi/Halo Formation History Project/Codes/Find V_max.py", line 222, in <module>
formation_def()
File "C:/Users/Khary/Documents/Astrophysics/Bolshoi/Halo Formation History Project/Codes/Find V_max.py", line 129, in formation_def
FT = np.loadtxt(TF,skiprows=0)
File "C:\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 770, in loadtxt
first_line = next(fh)
UnsupportedOperation: not readable
我的代码
import numpy as np
import temp
#large arrays of data
Id = np.array([some size])
MASS = np.array([some size])
V = np.array([some size])
def filesave(MAS,V): #Functioin to write and save values to file
Mc = str(MAS)
Vel = str(V)
w.write(Mc)
w.write('\t')
w.write(Vel)
w.write('\n')
return()
def formation_def():
count = 1
l =len(ID)
for i in range(l):
if ID[i] == count:
for j in range(i,l):
TF = tempfile.NamedTemporaryFile(mode='a')
if ID[j] <= ID[i]:
T = str(ID[j])
M = str(MASS[j])
Vel = str(V[j])
TF.write(T)
TF.write('\t')
TF.write(M)
TF.write('\t')
TF.write(Vel)
TF.write('\n')
elif ID[j]>ID[i]: # if ID[j]>TID[i] then we are in the next halo in the list
FT = np.loadtxt(TF,skiprows=0)
MASS2 = FT[:,0]
V2 = FT[:,2]
vel_max = np.argmax(V2)
filesave(MASS2[vel_max],V2[vel_max])
TF.close()
count+=1 # and must indcrement the counter and break out of loop
break
elif:
count = ID[i]+1
return()
当您创建临时文件时,您指定它向文件追加数据。也就是写操作。
TF = tempfile.NamedTemporaryFile(mode='a')
似乎失败的地方,您正在尝试从文件中读取数据并将其放入 FT。
FT = np.loadtxt(TF,skiprows=0)
把TF改成mode='r',运气应该会好一些。
我有三个数据数组。如果满足某些条件,我想循环它们并将值保存在临时文件中。如果条件不满足我想打开临时文件并找到最大值的索引然后保存到另一个文件。当我尝试下面的代码时,出现此错误。这是我第一次使用 tempfile.NamedTemporaryFile()
,所以我很可能没有正确使用它。谢谢
Traceback (most recent call last):
File "<ipython-input-19-7c44ca7dcbd6>", line 1, in <module>
runfile('C:/Users/Khary/Documents/Astrophysics/Bolshoi/Halo Formation History Project/Codes/Find V_max.py', wdir='C:/Users/Khary/Documents/Astrophysics/Bolshoi/Halo Formation History Project/Codes')
File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "C:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "C:/Users/Khary/Documents/Astrophysics/Bolshoi/Halo Formation History Project/Codes/Find V_max.py", line 222, in <module>
formation_def()
File "C:/Users/Khary/Documents/Astrophysics/Bolshoi/Halo Formation History Project/Codes/Find V_max.py", line 129, in formation_def
FT = np.loadtxt(TF,skiprows=0)
File "C:\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 770, in loadtxt
first_line = next(fh)
UnsupportedOperation: not readable
我的代码
import numpy as np
import temp
#large arrays of data
Id = np.array([some size])
MASS = np.array([some size])
V = np.array([some size])
def filesave(MAS,V): #Functioin to write and save values to file
Mc = str(MAS)
Vel = str(V)
w.write(Mc)
w.write('\t')
w.write(Vel)
w.write('\n')
return()
def formation_def():
count = 1
l =len(ID)
for i in range(l):
if ID[i] == count:
for j in range(i,l):
TF = tempfile.NamedTemporaryFile(mode='a')
if ID[j] <= ID[i]:
T = str(ID[j])
M = str(MASS[j])
Vel = str(V[j])
TF.write(T)
TF.write('\t')
TF.write(M)
TF.write('\t')
TF.write(Vel)
TF.write('\n')
elif ID[j]>ID[i]: # if ID[j]>TID[i] then we are in the next halo in the list
FT = np.loadtxt(TF,skiprows=0)
MASS2 = FT[:,0]
V2 = FT[:,2]
vel_max = np.argmax(V2)
filesave(MASS2[vel_max],V2[vel_max])
TF.close()
count+=1 # and must indcrement the counter and break out of loop
break
elif:
count = ID[i]+1
return()
当您创建临时文件时,您指定它向文件追加数据。也就是写操作。
TF = tempfile.NamedTemporaryFile(mode='a')
似乎失败的地方,您正在尝试从文件中读取数据并将其放入 FT。
FT = np.loadtxt(TF,skiprows=0)
把TF改成mode='r',运气应该会好一些。