Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在标题中搜索数据_Python - Fatal编程技术网

Python 在标题中搜索数据

Python 在标题中搜索数据,python,Python,我目前有一个脚本,可以从文件orderedfile.txt中读取数据,该文件具有如下所示的标题布局和数据布局。现在我需要一个python脚本来搜索头文件中的参数a、b、c和精细fft网格参数(本例中为25300300) 头文件的布局可能会略有变化,因此我认为最好不要考虑每个头文件的位置来处理这个问题。相反,我认为最好的方法是在标题中搜索短语“a=”,然后将其后面的内容设置为等于。。。然而,我不知道如何实现这一点。有人有什么建议吗 BEGIN header Real Lattice

我目前有一个脚本,可以从文件
orderedfile.txt
中读取数据,该文件具有如下所示的标题布局和数据布局。现在我需要一个python脚本来搜索头文件中的参数a、b、c和精细fft网格参数(本例中为25300300)

头文件的布局可能会略有变化,因此我认为最好不要考虑每个头文件的位置来处理这个问题。相反,我认为最好的方法是在标题中搜索短语“a=”,然后将其后面的内容设置为等于。。。然而,我不知道如何实现这一点。有人有什么建议吗

BEGIN header

       Real Lattice(A)               Lattice parameters(A)    Cell Angles
   2.4675850   0.0000000   0.0000000     a =    2.467585  alpha =   90.000000
   0.0000000  30.0000000   0.0000000     b =   30.000000  beta  =   90.000000
   0.0000000   0.0000000  30.0000000     c =   30.000000  gamma =   90.000000

 1                            ! nspins
25   300   300                ! fine FFT grid along <a,b,c>
END header: data is "<a b c> pot" in units of Hartrees

 1     1     1            0.042580
 1     1     2            0.049331
 1     1     3            0.038605
 1     1     4            0.049181
beginheader
实晶格(A)晶格参数(A)胞角
2.4675850 0.0000000 0.0000000 a=2.467585α=90.000000
0.0000000 30.0000000 0.0000000 b=30.000000β=90.000000
0.0000000.0000000 30.0000000 c=30.000000伽马=90.000000
1.nspins
25   300   300                ! 精细FFT网格
结束标题:数据是以哈特里斯为单位的“pot”
1     1     1            0.042580
1     1     2            0.049331
1     1     3            0.038605
1     1     4            0.049181

如果可以更改文件格式,请将其更改为更易于机器读取的格式(例如JSON)。如果不能,并且希望对格式进行未经通知的更改,请严格检查格式,否则很容易误解文件

为了严格检查这些格式,我经常以一种非常懒惰(但可读)的方式使用正则表达式

重新导入
def parse_头(f):
l=下一个(f)
m=重新匹配(r'开始标题',l)
断言m,l
l=下一个(f)
m=重新匹配(r'',l)
断言m,l
l=下一个(f)
m=重新匹配(r'实晶格\(A \)晶格参数\(A \)单元角',l)
断言m,l
l=下一个(f)
m=re.match(r'.*a=+(?P[0-9.+-]+)+.*,l)
断言m,l
a=浮动(m.group('a'))
l=下一个(f)
m=re.match(r'.*b=+(?P[0-9.+-]+)+.*,l)
断言m,l
b=浮动(m.group('b'))
l=下一个(f)
m=re.match(r'.*c=+(?P[0-9.+-]+)+.*,l)
断言m,l
c=浮动(m.group('c'))
l=下一个(f)
m=重新匹配(r'',l)
断言m,l
l=下一个(f)
m=重新匹配(r'.*nspins.*',l)
断言m,l
l=下一个(f)
m=重新匹配(r'.*(?P[0-9]+)+(?P[0-9]+)+(?P[0-9]+)+!沿',l的精细FFT网格)
断言m,l
ffta=int(m.group('ffta'))
fftb=int(m.group('fftb'))
fftc=int(m.group('fftc'))
l=下一个(f)
m=重新匹配(r'端头:数据为“pot”,单位为Hartrees',l)
断言m,l
l=下一个(f)
返回a、b、c、ffta、fftb、fftc
#准备测试“文件”
s=''开始标题
实晶格(A)晶格参数(A)胞角
2.4675850 0.0000000 0.0000000 a=2.467585α=90.000000
0.0000000 30.0000000 0.0000000 b=30.000000β=90.000000
0.0000000.0000000 30.0000000 c=30.000000伽马=90.000000
1.nspins
25   300   300                ! 精细FFT网格
结束标题:数据是以哈特里斯为单位的“pot”
1     1     1            0.042580
1     1     2            0.049331
1     1     3            0.038605
1     1     4            0.049181'''
#文件式可编辑
f=iter(s.split(“\n”))
a、 b,c,ffta,fftb,fftc=parse_头(f)
#继续处理该文件并处理剩余的数据行
对于f中的行:
打印(行)

当你说定位可能会改变时,你是指间距/行数吗?所有这些值的顺序是否相同?是的,顺序应保持一致。
import re

def parse_header(f):
    l=next(f)
    m = re.match(r'BEGIN header', l)
    assert m, l
    l=next(f)
    m = re.match(r'', l)
    assert m, l
    l=next(f)
    m = re.match(r'       Real Lattice\(A\)               Lattice parameters\(A\)    Cell Angles', l)
    assert m, l
    l=next(f)
    m = re.match(r'.*a = +(?P<a>[0-9.+-]+) +.*', l)
    assert m, l
    a = float(m.group('a'))
    l=next(f)
    m = re.match(r'.*b = +(?P<b>[0-9.+-]+) +.*', l)
    assert m, l
    b = float(m.group('b'))
    l=next(f)
    m = re.match(r'.*c = +(?P<c>[0-9.+-]+) +.*', l)
    assert m, l
    c = float(m.group('c'))
    l=next(f)
    m = re.match(r'', l)
    assert m, l
    l=next(f)
    m = re.match(r'.*nspins.*', l)
    assert m, l
    l=next(f)
    m = re.match(r'.*(?P<ffta>[0-9]+) +(?P<fftb>[0-9]+) +(?P<fftc>[0-9]+) +! fine FFT grid along <a,b,c>', l)
    assert m, l
    ffta = int(m.group('ffta'))
    fftb = int(m.group('fftb'))
    fftc = int(m.group('fftc'))
    l=next(f)
    m = re.match(r'END header: data is "<a b c> pot" in units of Hartrees', l)
    assert m, l
    l=next(f)
    return a,b,c,ffta,fftb,fftc

# prepare a test 'file'
s = '''BEGIN header

       Real Lattice(A)               Lattice parameters(A)    Cell Angles
   2.4675850   0.0000000   0.0000000     a =    2.467585  alpha =   90.000000
   0.0000000  30.0000000   0.0000000     b =   30.000000  beta  =   90.000000
   0.0000000   0.0000000  30.0000000     c =   30.000000  gamma =   90.000000

 1                            ! nspins
25   300   300                ! fine FFT grid along <a,b,c>
END header: data is "<a b c> pot" in units of Hartrees

 1     1     1            0.042580
 1     1     2            0.049331
 1     1     3            0.038605
 1     1     4            0.049181'''

# file like iterable
f=iter(s.split("\n"))


a,b,c,ffta,fftb,fftc = parse_header(f)
# continue with the file and handle remaining data lines
for line in f:
    print(line)