Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7603605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:41:59+00:00 2026-05-30T23:41:59+00:00

I have been given some ‘reports’ from another piece of software that contains data

  • 0

I have been given some ‘reports’ from another piece of software that contains data that I need to use. The file is quite simple. It has a description line that starts with a # that is the variable name/description. Followed by comma seperated data on the next line.

eg

    #wavelength,'<a comment describing the data>'
    400.0,410.0,420.0, <and so on>
    #reflectance,'<a comment describing the data>'
    0.001,0.002,0.002, <and so on>
    #date,'time file was written'
    2012-03-06 13:12:36.694597  < this is the bit that stuffs me up!! >

When I first typed up some code I expected all the data to be read as floats. But I have discovered some dates and strings. For my purposes All I care about is the data that should be arrays of floats. Everything else I read in (such as dates) can be treated as a strings (even if they are technically a date for example).

My first attempt – which worked until I found non-floats – basically ignores the # then grabs the chars proceeding it making a dictionary with the Key that is the chars it just read. Then I made the entry for the key an array by splitting on the commas and stacking on rows for 2-d data. Similar to the next section of code.

    data = f.readlines()
    dataLines = data.split('\n')

    for i in range(0,len(dataLines)-1):
        if dataLines[i][0] == '#':
            key,comment = dataLines[i].split(',')
            keyList.append(key[1:])
            k+=1
        else: # it must be data
            d+=1
            dataList.append(dataLines[i])

        for j in range(0,len(dataList)):
            tmp = dataList[j]

            x = map(float,tmp.split(','))
            tempData = vstack((tempData,asarray(x)))

    self.__report[keyList[k]] = tempData  

When I find a non-float in my file the line “x = map(float,tmp.split(‘,’))” fails (there are no commas in the line of data). I thought I would try and test if it is a string or not using isinstance but the file reader treats all of the data coming in from the file as a string (of course). I tried trying to convert the line from the file to a float array, thinking if it fails then just treat it as an array of strings – like this.

     try:
         scipy.array(tmp,dtype=float64)  #try to convert
         x = map(float,tmp.split(','))

     except:# ValueError: # must be a string
         x = zeros((1,1))
         x = asarray([tmp])
         #tempData = vstack((tempData,asarray(x)),dtype=str)
         if 'tempData' in locals():
             pass
         else:
             tempData = zeros((len(x)))

         tempData = vstack((tempData,asarray(x)))

This however results as EVERYTHING being read in as a character array and as such, I cannot index the data as a numpy array. All of the data is there in the dictionary but the dtype is s|8, for example. It seems the try block is going straight to the exception.

I would appreciate any advice on getting this to work so I can discriminate between floats and strings. I don’t know the order of the data before I get the report.

Also, the big files can take quite a long time to load in to memory, any advice on how to make this more efficient would also be appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:42:01+00:00Added an answer on May 30, 2026 at 11:42 pm

    I’m assuming that finally you are interested in the x which should be in the format [400.0, 410.0, 420.0].

    One way to handle this is separating the splitting by command and converting to float operations in two different statements, so that you can catch ValueError when you get string elements instead of float or int.

    keyList = []
    dataList = []
    with open('sample_data','r') as f:
        for line in f.readline():
            if line.startswith("#"):
                key, comment = line.split(',')
                keyList.append(key[1:])
            else: # it must be data
                dataList.append(line)
    
    for data in dataList:
        data_list = data.split(',')
        try:
            x = map(float, data_list)
        except ValueError:
            pass
    

    Also notice other minor changes that I’ve done to your code which makes it more pythonic in nature.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been given a MS-SQL database to gleen some reports from. However on
I have been given a textfile which contains data on time sheet. That line
I have been given a task of inputting some student data, like option 1:
I've been given some XML files that don't quite have a proper schema (I
I have been noticing that some software engineering jobs are asking for .NET developers,
I have been given some basic documentation of the RazorGator REST API and need
We have been given some code that does a URLRequest call which is really
The project I have been given is to store and retrieve unstructured data from
I have been given some html that look like this (I have no control
I have been given the task of re-writing some libraries written in C# so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.