用Python读取块数据?

时间:2009-07-17 01:35:16

标签: python file

我在读取数据文件时遇到问题:

///
* ABC Names
A-06,B-18,
* Data
 1.727e-01, 1.258e-01, 2.724e-01, 2.599e-01,-3.266e-01,-9.425e-02,-6.213e-02, 1.479e-01,
 1.219e-01, 1.174e-01, 2.213e-01, 2.875e-01,-2.306e-01,-3.900e-03,-5.269e-02, 7.420e-02,
 2.592e-01, 2.513e-01, 2.242e-01, 2.620e-01,-1.346e-01,-6.844e-02,-4.139e-02, 9.502e-02,
 1.981e-01, 1.937e-01, 2.336e-01, 1.617e-01,-4.240e-02, 2.285e-02, 1.878e-02, 1.064e-01,
 9.562e-02, 6.727e-02, 1.135e-01, 6.765e-02,-7.850e-02, 6.711e-02, 1.317e-02, 8.367e-02, 
* Starting position
     -.5000E+01
///

用Python运行代码?我尝试使用readline()readlines()函数,但没有结果。

6 个答案:

答案 0 :(得分:2)

以下是对可能加载文件类型的一些代码的完整猜测,这是一个例子,但应该有点健壮:

f = open("mdata.txt")

data_dict = {}
section = None
data_for_section = ""
for line in f:
    line = line.strip() #remove whitespace at start and end

    if section != None and (line[0] == "*" or line == "///"):
        # if we've just finished a section, put whatever we got into the data dict
        data_dict[section] = [bit for bit in data_for_section.split(",") if bit != ""]

    if line[0] == "*":
        # "*" denotes the start of a new section, probably, so remember the name
        section = line [2:]
        data_for_section = ""
        continue
    data_for_section += line

f.close()
#got the data, now for some output
print "loaded file. Found headings: %s"%(", ".join(data_dict.keys()))

for key in data_dict.keys():
    if len(data_dict[key])>5:
        print key, ": array of %i entries"%len(data_dict[key])
    else:
        print key, ": ", data_dict[key]

为您的文件输出:

loaded file. Found headings: ABC Names, Data, Starting position
ABC Names :  ['A-06', 'B-18']
Data : array of 40 entries
Starting position :  ['-.5000E+01']

当然,您可能希望在数据和起始位置的情况下将数据字符串列表转换为浮点数:

startingPosition = float(data_dict["Starting position"][0])
data_list_of_floats = map(float, data_dict["Data"])

但是对于ABC名称以及它们如何与文件的其余部分结合起来,我们需要更多信息。

答案 1 :(得分:1)

这应该适用于块名为'a','b'和'c'的文件。它将创建一个字典,其中键作为块标题,如下所示:

{'a':['line1','line2'],'b':['line1'],'c':['line1','line2','line3']} 

代码:

block_names = ['b','a','c']

for line in open('file.txt'):
    block_dict = {}  #dict to populate with lists of lines
    block = []  # dummy block in case there is data or lines before first block
    ck_nm = [blk_nm for blk_nm in block_names if line.startswith(blk_nm)]  #search block names for a match
    if ck_nm: # did we find a match?
        block_dict[ck_nm[0]] = block = []  # set current block
    else:
        block.append(line)  #..or line.split(',') ..however you want to parse the data

答案 2 :(得分:0)

假设文件名为“abc.txt”并且在当前目录中;然后是以下Python脚本:

f = open("abc.txt")
all_lines = f.readlines()

会将所有行读入字符串列表all_lines,每个字符串的结尾为\n,并且全部为。

除非你告诉我们,否则我们无法猜测你想要做什么,但是你要问的部分,阅读应该得到满足。

答案 3 :(得分:0)

假设您想要从*数据到*起始位置,

f=0
for line in open("file"):
    line=line.strip()
    if "Starting" in line:
        f=0
    if "Data" in line:
        f=1
        continue
    if f:
        print line

这个想法是设置一个标志。如果*数据被击中,则设置标志。如果设置了标志,则打印所有行。如果点击* Starting,请关闭标记。

答案 4 :(得分:0)

没有任何其他信息......

data = [
1.727e-01, 1.258e-01, 2.724e-01, 2.599e-01,-3.266e-01,-9.425e-02,-6.213e-02, 1.479e-01,
1.219e-01, 1.174e-01, 2.213e-01, 2.875e-01,-2.306e-01,-3.900e-03,-5.269e-02, 7.420e-02,
2.592e-01, 2.513e-01, 2.242e-01, 2.620e-01,-1.346e-01,-6.844e-02,-4.139e-02, 9.502e-02,
1.981e-01, 1.937e-01, 2.336e-01, 1.617e-01,-4.240e-02, 2.285e-02, 1.878e-02, 1.064e-01,
9.562e-02, 6.727e-02, 1.135e-01, 6.765e-02,-7.850e-02, 6.711e-02, 1.317e-02, 8.367e-02,
]

答案 5 :(得分:0)

感谢您的回复。但是,我尝试从Markus的代码开始,但是当此行和该行之间存在空时失败。 例: /// “filename.txt”有: * ABC名称 A-06,B-18, *数据  1.727e-01,1.2558e-01,2.724e-01,2.599e-01,-3.266e-01,-9.425e-02,-6.213e-02,1.479e-01,  1.219e-01,1.174e-01,2.213e-01,2.875e-01,-2.306e-01,-3.900e-03,-5.269e-02,7.420e-02,  2.592e-01,2.513e-01,2.242e-01,2.620e-01,-1.346e-01,-6.844e-02,-4.139e-02,9.502e-02,  1.981e-01,1.937e-01,2.3336e-01,1.617e-01,-4.240e-02,2.285e-02,1.878e-02,1.064e-01,  9.562e-02,6.727e-02,1.135e-01,6.765e-02,-7.850e-02,6.711e-02,1.317e-02,8.367e-02, * 起始位置      -.5000E + 01

  • 总点击次数(已修改)   18

  • 点击次数(已修改) 448 748 /// “起始位置”和“总点击次数(已修改)”之间出现空行时出错。 我需要数据: 1.727e-01,1.2558e-01,2.724e-01,2.599e-01,-3.266e-01,-9.425e-02,-6.213e-02,1.479e-01, 1.219e-01,1.174e-01,2.213e-01,2.875e-01,-2.306e-01,-3.900e-03,-5.269e-02,7.420e-02, 2.592e-01,2.513e-01,2.242e-01,2.620e-01,-1.346e-01,-6.844e-02,-4.139e-02,9.502e-02, 1.981e-01,1.937e-01,2.3336e-01,1.617e-01,-4.240e-02,2.285e-02,1.878e-02,1.064e-01, 9.562e-02,6.727e-02,1.135e-01,6.765e-02,-7.850e-02,6.711e-02,1.317e-02,8.367e-02, 在块“*数据” 并且有时需要数据
    448 748 在块“*点击次数(修改)”