簡體   English   中英

如何將數據從.txt文件導入python中的數組

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

我正在嘗試從.txt文件中導入數據,該文件包含四列,各列由制表符分隔,並且長度為數千行。 這是文檔開頭的樣子:

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

我有幾個要循環顯示的文件,並存儲在一個單元格/列表中,每個單元格/列表項都包含四列。 之后,我只需要使用該單元格/列表來循環繪制數據。

我看到熊貓圖書館很合適,但我不知道如何使用它。

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

這是我嘗試過的無效的方法,真的不知道如何進行。 我知道有一些有關此問題的文檔,但是我對此並不陌生,需要一些幫助。

嘗試上述操作時出現錯誤:

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

因此,這是一個簡單的修復,只需要從skiprows=[19]刪除括號skiprows=[19]

鱈魚現在看起來像這樣並且可以工作。

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