我目前正在努力处理一个小块,它应该筛选一个大文件并只写出满足某个功能限制的特定数据。我主要是格式化问题:
from __future__ import print_function
import numpy as np
log = open("out_test", "w")
log1 = open("out_test_empty", "w")
def function(kx,ky,ek,sz,ef,tol):
lines = []
if ((ef-tol)<ek<(ef+tol)):
lines.append((kx,ky,sz))
# (lines.append((kx,ky,sz)) if (kx,ky,sz) is not None else None)
return lines
arr = []
with open('test10') as my_file:
for line in my_file:
##numbers_float=map(float, line.split())
arr.append(map(float, line.split()))
print(function(arr[-1][0],arr[-1][1],arr[-1][2],arr[-1][3],-0.5462,0.001), file = log)
# print(', '.join(str(d) for d in function(arr[-1][0],arr[-1][1],arr[-1][2],arr[-1][3],-0.5462,0.001)), file = log)
# str_list = (', '.join(str(d) for d in function(arr[-1][0],arr[-1][1],arr[-1][2],arr[-1][3],-0.7,0.001)))
# str_list = filter(None, str_list)
# print(str_list, file =log)
#print(arr[:][:])
#import fileinput
#for line in fileinput.FileInput("test",inplace=1):
# if line.rstrip():
# print(line, file=log1)
测试数据文件(大约10 ^ 7行):
0.11361097E+01 0.19677992E+01 -0.54626220E+00 -0.87053581E+00
0.11361097E+01 0.19677992E+01 -0.54626220E+00 0.87053581E+00
0.11361097E+01 0.19677992E+01 0.22336976E+01 0.90485914E+00
0.11361097E+01 0.19677992E+01 0.22336976E+01 -0.90485916E+00
0.11361097E+01 0.19677992E+01 0.23120941E+01 -0.42934308E+00
0.11361097E+01 0.19677992E+01 0.23120941E+01 0.42934308E+00
0.11361097E+01 0.19677992E+01 0.24740033E+01 -0.98827766E+00
0.11361097E+01 0.19677992E+01 0.24740033E+01 0.98827766E+00
0.11361097E+01 0.19677992E+01 0.24777489E+01 -0.52602145E+00
0.11361097E+01 0.19677992E+01 0.24777489E+01 0.52602145E+00
我想要的文件输出是:
1.1361097 1.9677992 -0.87053581
1.1361097 1.9677992 0.87053581
现在我正在接受:
[(1.1361097, 1.9677992, -0.87053581)]
[(1.1361097, 1.9677992, 0.87053581)]
[]
[]
[]
[]
[]
[]
[]
[]
所以我的问题是:
我知道这些问题经常被问到,我试图解决它,但我无法解决它。在评论的部分,你会看到我已经尝试过的......
答案 0 :(得分:1)
您的函数返回元组列表:
def function(kx,ky,ek,sz,ef,tol): lines = [] if ((ef-tol)<ek<(ef+tol)): lines.append( (kx,ky,sz) ) # emphasis: this appends a tuple to the list return lines
以(kx,ky,sz)
为元组。只需将数字作为浮动列表返回:
def function(kx,ky,ek,sz,ef,tol):
if ((ef-tol)<ek<(ef+tol)):
return [kx,ky,sz]
# implicitly return None if your condition is not met
如果您想将它们打印为不是列表而是打印为单个值,请在打印前分解function(...)
返回的列表:
# error when decomposing None - you would have to return [] instead of None implicitly
print(* function(arr[-1][0],arr[-1][1],arr[-1][2],arr[-1][3],-0.5462,0.001), file = log)
你会得到新的/错误,所以也许最好将部分写入你的其他文件:
newVals = function(arr[-1][0],arr[-1][1],arr[-1][2],arr[-1][3],-0.5462,0.001)
if newVals: # meaning: if the function returns something, print its elements
print(*newVals, file = log)
很好阅读how to debug small programs (#2) - 可以在该链接中找到 - python非常“打印出友好”,有时您只需要保留更多变量来打印它们并查看结果如何建造。