简体   繁体   English

如何将数据从.txt文件导入python中的数组

[英]How to import data from a .txt file into arrays in python

I am trying to import data from a .txt file that contains four columns that are separated by tab and is several thousands lines long. 我正在尝试从.txt文件中导入数据,该文件包含四列,各列由制表符分隔,并且长度为数千行。 This is how the start of the document look like: 这是文档开头的样子:

Data info
File name: D:\(path to file)
Start time: 6/26/2019 15:39:54.222
Number of channels: 3
Sample rate: 1E6
Store type: fast on trigger
Post time: 20
Global header information: from DEWESoft
Comments: 

Events
Event Type  Event   Time    Comment
1   storing started at  7.237599    
2   storing stopped at  7.257599    


Data1
Time    Incidente   Transmitida DI 6    
s   um/m    um/m    -   
0   2.1690152   140.98599   1
1E-6    2.1690152   140.98599   1
2E-6    4.3380303   145.32402   1
3E-6    4.3380303   145.32402   1
4E-6    -2.1690152  145.32402   1

I have several of these files that I want to loop trough and store in a cell/list that each cell/list item contains the four columns. 我有几个要循环显示的文件,并存储在一个单元格/列表中,每个单元格/列表项都包含四列。 After that I just use that cell/list to plot the data with a loop. 之后,我只需要使用该单元格/列表来循环绘制数据。

I saw that pandas library was suitable, but I don't understand how to use it. 我看到熊猫图书馆很合适,但我不知道如何使用它。

fileNames = (["Test1_0001.txt", "Test2_0000.txt", "Test3_0000.txt",
    "Test4_0000.txt", "Test5_0000.txt", "Test6_0001.txt", "Test7_0000.txt",
    "Test8_0000.txt", "Test9_0000.txt", "Test10_0000.txt", "RawblueMat_0000.txt"])

folderName = 'AuxeticsSHPB\\' #Source folder for all files above

# Loop trough each source document
for i in range(0,len(fileNames)):
    print('File location: '+folderName+fileNames[i])

    # Get data from source as arrays, cut out the first 20 lines
    temp=pd.read_csv(folderName+fileNames[i], sep='\t', lineterminator='\r', 
                     skiprows=[19], error_bad_lines=False)

    # Store data in list/cell
    # data[i] = temp   # sort it

This is something I tried that didn't work, don't really know how to proceed. 这是我尝试过的无效的方法,真的不知道如何进行。 I know there are some documentation on this problem but I am new to this and need some help. 我知道有一些有关此问题的文档,但是我对此并不陌生,需要一些帮助。

An error I get when trying the above: 尝试上述操作时出现错误:

ParserError: Error tokenizing data. C error: Expected 1 fields in line 12, saw 4

So it was an easy fix, just had to remove the braces from skiprows=[19] . 因此,这是一个简单的修复,只需要从skiprows=[19]删除括号skiprows=[19]

The cods now looks like this and works. 鳕鱼现在看起来像这样并且可以工作。

fileNames = ["Test1_0001.txt", "Test2_0000.txt", "Test3_0000.txt",
    "Test4_0000.txt", "Test5_0000.txt", "Test6_0001.txt", "Test7_0000.txt",
    "Test8_0000.txt", "Test9_0000.txt", "Test10_0000.txt", "RawblueMat_0000.txt"]

folderName = 'AuxeticsSHPB\\' #Source folder for all files above

# Preallocation
data = []

for i in range(0,len(fileNames)):
    temp=pd.read_csv(folderName+fileNames[i], sep='\t', lineterminator='\r', 
                     skiprows=19)
    data.append(temp)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM