简体   繁体   English

如何从Python中以空格分隔的.DAT文件中提取多个列

[英]How to extract multiple columns from a space delimited .DAT file in python

I'm quite new to coding and don't have a proper education on the subject (most of my experience has been just stumbling through google searches) and I have a task that I would like assistance with. 我对编码还很陌生,并且没有对该主题进行适当的教育(我的大部分经验只是在Google搜索上绊脚石),并且我有一项需要帮助的任务。

I have 38 files which look something like this: 我有38个看起来像这样的文件:

NGANo: 000a16d_1 NGANo:000a16d_1

Zeta: 0.050000 Zeta:0.050000

Ds5-95: 5.290000 DS5-95:5.290000

Comments: 评论:

Period, SD, SV, SA 周期,SD,SV,SA

0.010000 0.000433 0.013167 170.812839 0.010000 0.000433 0.013167 170.812839
0.020000 0.001749 0.071471 172.720229 0.020000 0.001749 0.071471 172.720229
0.030000 0.004014 0.187542 176.055129 0.030000 0.004014 0.187542 176.055129
0.040000 0.007631 0.468785 189.322248 0.040000 0.007631 0.468785 189.322248
0.050000 0.012815 0.912067 203.359441 0.050000 0.012815 0.912067 203.359441
0.060000 0.019246 1.556853 210.602517 0.060000 0.019246 1.556853 210.602517
0.070000 0.025400 1.571091 206.360018 0.070000 0.025400 1.571091 206.360018

They're all .DAT files and are four columns of data (Period, SD, SV, SA) that are single space delimited in each row, additionally there are two spaces at the end of each line of data. 它们都是.DAT文件,是四列数据(句点,SD,SV,SA),每行用单个空格分隔,此外,每行数据的末尾还有两个空格。

The only important data for me is the SA data, and I'd like to take the SA data and the title (this particular example being 000a16d_1) from each of these 38 files and put them all on the same sheet of an excel spreadsheet (one column after the next) with just the title followed by the SA data. 对我来说,唯一重要的数据是SA数据,我想从这38个文件中的每个文件中获取SA数据和标题(此特定示例为000a16d_1),并将它们全部放在excel电子表格的同一张纸上(下一列之后的一列),仅包含标题和SA数据。

I've tried a few different things, but I'm stuck on how to separate the rows of data from one column into 4. I'm not too knowledgeable on whether I should use numpy or pandas. 我尝试了几种不同的方法,但是我仍然坚持如何将数据行从一列分离为4。我不太了解应该使用numpy还是pandas。 I know that everything up to the second to last line is correct, as when I have print(table) it does print the rows of data, I just don't understand how to separate the single column into multiple. 我知道直到倒数第二行的所有内容都是正确的,因为当我使用print(table)时,它确实打印了数据行,我只是不知道如何将单列分成多个。 Here is my current code, all assistance is appreciated. 这是我当前的代码,感谢您的协助。

import pandas as pd
import numpy as np
import os
import xlsxwriter
#
path = "C:/Users/amihi/Downloads/Plotter_Output"
dirs = os.listdir(path)
#
#
for file in dirs:
    table = pd.read_table(file, skiprows=4)
    SA = table.loc[:,"SA"]
    print(SA)

You could also do this without using pandas if you wanted. 如果需要,您也可以不使用熊猫来执行此操作。 The code below will deal only with the table section of it, but wont deal with the info at the top of the file. 下面的代码将仅处理其表部分,而不会处理文件顶部的信息。

finalColumns = []
for file in dirs:
    with open(file, "r") as f:
        for l in f:
            line = l.strip("\n")
            splitted = line.split()
            if len(splitted) > len(columns):
                 for i in range(len(splitted)):
                     columns.append([])
            counter = 0
            for item in splitted:
                columns[counter].append(item)
                counter += 1   
        finalColumns.append(columns[3])

When adding to your other file, simply loop through finalColumns and each item will be what should be a new column in your file. 当添加到其他文件时,只需遍历finalColumns即可,每个项目将成为文件中的新列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python从文件的列中读取由空格分隔的数组 - Python read arrays delimited by white space from columns in a file 如何在Python中从空格分隔的文件中提取特定的列? - How to extract specific columns from a space separated file in Python? 如何在python中读取扩展名为.dat的文件并从中提取数据 - How to read a file of extension .dat in python and extract data from it 在Python中,如何从空格分隔的.txt文件中获取整数列表,并在多行上使用'\\ r \\ n'分隔数字? - In Python, how to get integer lists from a .txt file with space separated and '\r\n' delimited numbers on multiple lines? 使用Python从多个文本文件中提取列 - extract columns from multiple text file with Python 如何将空格分隔的文件转换为 Python 中的 csv? - How to convert a space delimited file to a csv in Python? 从.DAT文件中提取大小不确定的多个数组 - Extract multiple arrays from .DAT file with undefined size 使用Python或Pandas,仅从txt或dat文件中提取字符串 - With Python or Pandas, extract only the strings from a txt or dat-file 如何在 Python 中从这个 .dat.gz 文件中打开和提取数据? - How do I open and extract data from this .dat.gz file in Python? 如何在使用 python 保存其形状的同时从 .dat 文件中提取表格? - How to extract a table from .dat file while conserving its shape using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM