The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Python
 Importing data using script but code not working?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

jasgel

1 Posts

Posted - 08/31/2021 :  04:36:34 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin):
Operating System: Windows
Data file:https://my.originlab.com/ftp/forum_and_kbase/Images/Rheology.xlsx

Hi, I'm trying to import some data (see attached) using something similar to what has been supplied in the Python for Origin video tutorial (https://www.youtube.com/watch?v=HVW-WThxzHk&list=PLAH66v11YrfD-_q9QaA-lnjCDdlzk1dHu&index=3&t=1581s&ab_channel=OriginLabCorp.).

I've pretty much just copied and pasted the script from the video and removed the code to process the first four lines (in the video script) and just used the rest of the script to import the rest of the data but it doesn't work? Is it due to my data having more than one column?

I am using the top line as my long name and the second line as the units. The rest of the code is to fill in the data sheet.

import originpro as op
import pandas as pd
import os
 
def read_file(file):  
    wks = op.find_sheet() # Get the active worksheet.
    
    with open(file, 'r') as f:
        # Read the Longname line
        line = f.readline()
        cols = line.split()
        
        # Read the Unit line
        line = f.readline()
        wks.set_labels([line], 'U')
        
        # Read in the data
        rows = []
        for line in f.readlines():
            rows.append(line.split())
 
    df = pd.DataFrame(data=rows, columns=cols)
    
    # Save data to worksheet
    wks.from_df(df)
    
    # Set column designation
    wks.cols_axis('XY') 
    
    # Set sheet name to file name
    wks.name = os.path.basename(file)
 
# The file chosen by Origin import filter is placed into the fname$
# LabTalk variable. Must bring it into Python.

fname = op.get_lt_str('fname')
data = read_file(fname)



Thanks in advance,

Jason

Edited by - jasgel on 08/31/2021 06:27:52 AM

YimingChen

1621 Posts

Posted - 08/31/2021 :  09:19:34 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You can't read Excel files as a text files using methods like readlines or read. Please use python modules like pyexcel or openpyxl to read .xlsx files.

James
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000