简体   繁体   中英

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. 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] .

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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