提取.txt文件数据,并把这些数据行和列

问题描述 投票:-3回答:2

我有一组在此格式的.txt文件DATAS的:

30     1
     2477.25     0.00    1                   M
40     2   11
        0.17100     0.08600     0.11500     0.10800     0.05600     0.07500 9.60000 -1009.00000 -1009.00000 -1009.00000
        2.70000
36     1    1
   a.a.Sbargang
30     1
     2477.45     0.00    2                   M
40     2   11
      0.52100     0.27400     0.35900 -1009.00000 -1009.00000 -1009.00000    14.30000 -1009.00000 -1009.00000 -1009.00000
      2.66000
36     1    1
   a.a M-gr.

该格式是相当混乱的,我希望把它的行和列,所以我的输出将是这样的:

30 1 2477.25 0.00 1 M 40 2 11 0.17100 0.08600 0.11500 0.10800 0.05600  0.07500 9.60000 -1009.00000 -1009.00000 -1009.00000 2.70000 36 1 1 a.a.Sbargang
30 1 2477.45 0.00 2 M 40 2 11 0.52100 0.27400 0.35900 -1009.0 -1009.0 -1009.00 14.3000 -1009.00000 -1009.00000 -1009.00000 2.66000 36 1 1 a.a M-gr.

我很新的Python和不知道如何写python3做这个任务?提前致谢

我曾尝试这样的:

with open ('textdata3.txt') as f:
    inputString = f.read()

inputString = re.sub(r" +"," ", inputString)
itemInString = inputString.split(" ")

row1 = []
for index, item in enumerate(itemInString):
    if index % 1 == 0:
    row1.append(str(item))

print(row1)

我不知道这是正确的做法,但在这里我得到了1行的所有内容。

输出:

['30', '1\n', '2477.25', '0.00', '1', 'M\n40', '2', '11\n', '0.17100', '0.08600', '0.11500', '0.10800', '0.05600', '0.07500', '9.60000', '-1009.00000', '-1009.00000', '-1009.00000\n', '2.70000\n36', '1', '1\n', 'Sst.Lt-gry.F-gr.Sbang.VW-cmt.VP-srt.w/Mic.Calc.Glauc.\n30', '1\n', '2477.45', '0.00', '2', 'M\n40', '2', '11\n', '0.52100', '0.27400', '0.35900', '-1009.00000', '-1009.00000', '-1009.00000', '14.30000', '-1009.00000', '-1009.00000', '-1009.00000\n', '2.66000\n36', '1', '1\n', 'a.a', 'M-gr.']
python python-3.x rows
2个回答
1
投票

假设数据以七行应该工作块一贯分组。

import re
rows = []
with open("input_data.txt", "rb") as input_file:
    while True:
        try:
            row = [str(next(input_file), "utf-8") for x in xrange(7)]
            rows.append(re.sub( '\s+', ' ', " ".join(row)))
        except StopIteration as e:
            break

with open("reformatted_data.txt", "wb") as out_file:
    for row in rows:
        out_file.write(row+"\n")

基于下面的评论更新版本。

import re
rows = []
with open("data.txt", "rb") as input_file:
    row = []
    while True:
        try:
            data = str(next(input_file))
            data = re.sub( '\s+', ' ', data).strip()
            if data == "30 1":
                rows.append(" ".join(row))
                row = []

            row.append(data)

        except StopIteration as e:
            rows.append(" ".join(row))
            break

with open("reformatted_data.txt", "wb") as out_file:
    for row in rows:
        out_file.write(row+"\r\n")

0
投票

出口,因为我结束了一个巨大的列,然后打破这一粗大的柱子重现原来的结构所需要的数据,当我有类似的问题。这个代码块解决我的问题:

def arrumando_dados():
    #defining a path to the file
    path_to = glob.glob('.../txts/*.txt')

    #creating an empty dictionary
    idl_results = {}

    #looping over the files
    for i in range(0,len(path_to)):

        #creating a variable with the appropriated name
        #that will only work if the numbers are positioning the cropping
        #correctly
        var_name = path_to[i][-6:-4]

        #taking the data with numpy
        data2 = np.loadtxt(path_to[i])

        #break lines every 949 items
        new_data = np.array(np.array_split(data2,949))

        #fixing for the idl vs python display
        new_data_t = np.matrix.transpose(new_data)

        #updating the dictionary
        idl_results.update({var_name: new_data_t})

    return(idl_results)

那么我认为有一些调整,你可以利用这段代码来解决你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.