简体   繁体   中英

Reading and splitting a .raw file for data processing

Basically I have data from a mechanical test in the output format .raw and I want to access it in Python.

The file needs to be splitted using delimiter ";" so it contains 13 columns. By doing this the idea is to index and pullout the desired information, which in my case is the "Extension mm" and "Load N" values as arrays in row 41 in order to create plot.

I have never worked with .raw files and I dont know what to do.

The file can be downloaded here: https://drive.google.com/file/d/0B0GJeyFBNd4FNEp0elhIWGpWWWM/view?usp=sharing

Hope somebody can help me out there!

Your file looks basically like a .tsv with 40 lines to skip. Could you try this ?

import csv

#export your file.raw to tsv
with open('TST0002.raw') as infile, open('new.tsv', 'w') as outfile:
    lines = infile.readlines()[40:]
    for line in lines:
        outfile.write(line)

Or if you want to make directly some data analysis on your two columns :

import pandas as pd

df = pd.read_csv("TST0002.raw", sep="\t", skiprows=40, usecols=['Extension mm', 'Load N'])

print(df)

output:

   Extension mm       Load N
0       -118.284    0.1365034
1       -117.779  -0.08668576
2       -117.274   -0.1142517
3       -116.773   -0.1092401
4       -116.271   -0.1144083
5        -11.577   -0.1314806
6       -115.269  -0.03609632
7       -114.768  -0.06334914
....

您可以将原始文件转换为csv文件,然后使用csv模块记住设置delimeter ='',否则默认情况下它将逗号作为delimeter

import csv with open('TST0002.csv', 'r') as csvfile: reader = csv.reader(csvfile, delimiter=' ') for row in reader: //this will read each row line by line print (row[0]) //you can use row[0] to get first element of that row.

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