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
 All Forums
 Origin Forum for Programming
 Forum for Python
 Importing data using script but code not working?

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
jasgel Posted - 08/31/2021 : 04:36:34 AM
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
1   L A T E S T    R E P L I E S    (Newest First)
YimingChen Posted - 08/31/2021 : 09:19:34 AM
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

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000