0

So i have a list of data like this:

"Load (lbf)","Time (s)","Extension (in)","Stress (ksi)","Strain (in/in)"

103.2,0.30,0.008,1.160,0.00228
172.4,0.50,0.009,1.939,0.00246
241.8,0.70,0.010,2.718,0.00264
311.2,0.90,0.010,3.500,0.00282...

What I am essentially trying to do is convert it to a tab separated table such that it can be copy-pasted into origin or excel. The plan to do this is read each column into an array and write it back with tabs as separation. The trouble is I have little formal training in writing code and I haven't done it in a few months, so reading around on SE isn't particularly helpful(its rare that people explain what their code does line by line.) What I'm running into is reading the data doesn't seem to want to play. I've tried:

file=str(input("enter filepath: "))
hdr=int(input("enter number of lines before data: "))
def read(file, convert=float, sep=","):
    with open(file) as f:
        for i in range(hdr):
            next(f)

        for line in f:
            load.append(col[1])

As well as:

file=str(input("enter filepath: "))
hdr=int(input("enter number of lines before data: "))
def read(file, convert=float, sep=","):
     with open(file) as f:
        for i in range(hdr):
            next(f)
        for line in f:

          extension.append[convert(line.split(sep=',')[3]) for line in f]

There are no errors thrown, but it simply does nothing. any tips would be greatly helpful.

1
  • well, did you call the function read in your script? you only just defined the function. Commented Sep 26, 2018 at 2:43

1 Answer 1

1

You might want to leverage on the pandas package, which specializes in handling and manipulating data. In particular, there is a read_csv function which takes in a csv and saves it as a DataFrame (Panda's data structure). Then you can re-output it as another csv, but this time, specifying the separation to be tabs \t.

import pandas as pd
data = pd.read_csv('data.csv')  # load dataset into a Pandas dataframe
data.head()  # inspect first 5 rows in your dataset to make sure all is well
data.to_csv('new_data.csv', sep='\t', index=False)  # output with tab separated values
Sign up to request clarification or add additional context in comments.

1 Comment

yes, as long as the data in the txt file is formatted in a consistent way. (separated by commas or tabs, etc.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.