0

This is in reference to my previous question related to extracting data from .asc file and separating them while having multiple delimiters.

I want to perform mathematical operations on the float elements of the list of lists generated from the above question. The separation of individual data from the string has been achieved however, since the list of lists has also generated individual elements in form of strings i am unable to perform mathematical operations on them.

I would like to be able to access each element in the list of lists, convert them to float type and then perform mathematical operations on them.

enter image description here

Here is my code where in the .asc file strings have been separated into individual elements and stored as list of lists.

This is the image of a specific set of datas i got from the bigger list of lists.

enter image description here

I access the specific set of data from the lists and then when i try to convert them to float, i get this error ValueError: could not convert string to float: '.'

This is the code i have been working with

import numpy as np
import pandas as pd
import re
Output_list = []
Final = []
count = 0
with open(r"myfile.asc","r") as file_in:
    for line in map(str.strip, file_in):
        if "LoggingString :=" in line:
            first_quote = line.index('"')  # returns the column number where '"' first appears in the
                                            # whole string
            last_quote = line.index('"', first_quote + 1)  #returns the column value where " appears last                                              
                                                            #in the # whole string ( end of line )
            Output_list.append(
                line[:first_quote].split(maxsplit=1)
                + line[first_quote + 1: last_quote].split(","),
            )
            Final.append(Output_list[count][8:25])
            Data = list(map(float, Output_list[count][8]))  #converting column 8th element of every lists 
                                                                       #in Output_list to float
            count += 1
df = pd.DataFrame(Output_list)
df.to_csv("Triall_2.csv", sep=';')
df_1 = pd.DataFrame(Final)
df_1.to_csv("Test.csv", sep=";")

I alternatively tried using np.array(Final).astype(float).tolist() method as well but it didn't change the strings to float as i wanted.

3
  • Have you tried Data = float(Output_list[count][8]) ? Commented Nov 29, 2022 at 13:49
  • @AndrejKesely this does return a float value. But it doesnt return a list. Instead there is only the last value for e.g from float(Output_list[24][8] stored in Data. How do i deal with this? Commented Nov 29, 2022 at 15:14
  • 1
    I've posted a pseudo-code how you can append the float values into an array. Commented Nov 29, 2022 at 17:20

2 Answers 2

1

Declare Data as empty list outside for-loop and use .append to insert a float value into it:

Data = []
with open(r"myfile.asc", "r") as file_in:
    for line in map(str.strip, file_in):
        if "LoggingString :=" in line:
            # ...
            Data.append(float(Output_list[count][8]))
            count += 1

print(Data)
Sign up to request clarification or add additional context in comments.

Comments

0

The problem occurs when trying to map an individual string '1.06' into a float object. It wil treat the string as an array and try to turn each individual element of the array into a float object, but the second element of this example is the dot character . which cannot be turned into a float object.

>>> my_array = ['0','1.06','23.345']
>>> list(map(float, my_array[1]))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: could not convert string to float: '.'

Instead it is more convenient to turn all elements of the array into float objects:

>>> my_array = ['0', '1.06', '23.345']
>>> list(map(float,my_array))
[0.0, 1.06, 23.345]

For more information you can look at the map documentation.

1 Comment

This doesn't work in my case because i have a list of lists.. hence map(float,x) doesn't work.. it always give TypeError to me in this case.... Thank you :)

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.