python - 从Python中的文本文件中读取数字

标签 python

我正在尝试从文本文件中读取一列数字,如下所示:

一些文字和数字...,然后:

 q-pt=    1    0.000000  0.000000  0.000000      1.0000000000
   1      -0.066408              0.0000000                      
   2      -0.053094              0.0000000                      
   3      -0.037643              0.0000000 
   ...
   156    3107.735577            6.8945617
...more text file

我有兴趣阅读第二列,其中包含 -0.066408、-0.053094 等。
我尝试编写的代码不知何故无法完成工作而不给出任何错误。我已经尝试过:

import re                                                                            
import sys                                                                           
from string import atof                                                              
from math import exp                                                                 
from numpy import *                                                                  

file1 = open('castepfreq.dat', 'w')                                                  
with open('xd_geo_Efield.phonon') as file:                                           
    File = file.readlines()                                                          
    p1 = re.compile("q-pt=    1    0.000000  0.000000  0.000000      1.0000000000")  
    for i in range(len(File)):                                                       
        m1 = p1.search(File[i])                                                      
          if  m1:                                                                       
            read = int(float(File[i+1][10:23]))      
            freq = (read)                                                            
    print >> file1, freq    
file1.close()

如果有人能帮助我解决这个问题,那就太好了。

最佳答案

您可以按空格分割,然后提取第二个元素:

with open('xd_geo_Efield.phonon') as f:
    col = [line.split()[1] for line in f]
    print(col)

如果您的输入是:

q-pt=    1    0.000000  0.000000  0.000000      1.0000000000
1      -0.066408              0.0000000
2      -0.053094              0.0000000
3      -0.037643              0.0000000

输出将是:

[('1', '-0.066408', '-0.053094', '-0.037643')]

或者使用 itertools 并转置:

from itertools import izip, islice, imap
with open('xd_geo_Efield.phonon') as f:
    col = islice(izip(*imap(str.split,f)), 1,2)
    print(list(col))

如果要转换,请将值转换为 float :

 [float(line.split()[1]) for line in f]

此外,如果您想跳过 header 并忽略 1,请在使用其余代码之前对文件对象调用 next(f),即:

with open('xd_geo_Efield.phonon') as f:
      next(f)
      col = [float(line.split()[1]) for line in f]
      print(list(col))

这会输出:

 [-0.066408, -0.053094, -0.037643]

如果您想要忽略并且仅从 q-pt=.. 行开始的数据,则可以使用 itertools.dropwhile 忽略开头的行:

from itertools import dropwhile

with open('xd_geo_Efield.phonon') as f:
    col = [float(line.split()[1]) for line in dropwhile(
           lambda x: not x.startswith("q-pt="), f)]
    print(list(col))

如果您还想忽略该行,可以再次调用 next,但这次是在 dropwhile 对象上:

from itertools import dropwhile

with open('xd_geo_Efield.phonon') as f:
    dp = dropwhile(lambda x: not x.startswith("q-pt="), f)
    next(dp)
    col = [float(line.split()[1]) for line in dp]
    print(list(col))

对于输入:

some 1 1 1 1 1
meta 2 2 2 2 2
data 3 3 3 3 3
and 4 4 4 4 4
numbers 5 5 5 5 5
q-pt=    1    0.000000  0.000000  0.000000      1.0000000000
1      -0.066408              0.0000000
2      -0.053094              0.0000000
3      -0.037643              0.0000000
3      -0.037643              0.0000000

输出将是:

[-0.066408, -0.053094, -0.037643, -0.037643]

对于前导空格,lstrip 将其关闭:

from itertools import dropwhile, imap, takewhile

with open('xd_geo_Efield.phonon') as f:
    # for python3 just use map
    dp = dropwhile(lambda x: not x.startswith("q-pt="), imap(str.lstrip,f))
    next(dp)
    col = [float(line.split(None,2)[1]) for line in takewhile(lambda x: x.strip() != "", dp)]
    print(list(col))

takewhile 将继续获取行,直到我们到达文件末尾的第一个空行。

关于python - 从Python中的文本文件中读取数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32767740/

相关文章:

python - 排序python字典

Python time.clock() - 使用线程重置时钟值

python - Apache mod_wsgi 和 python 2.7

python - 将 Dataframe 转换为具有列表值的字典

Python - 如何将 float 向下舍入到 1 位有效数字

python - 让 Fabric Python 库工作

java - 哪种编程语言用于计算密集型交易组合模拟?

按列将列表列表转换为字符串的pythonic转换

python - 在 OSX 上检查进程是否每 20 秒运行一次并杀死他

python - 在 python 2 和 3 的第一行打印