Home > Enterprise >  Error while converting String elements of List of Lists to float
Error while converting String elements of List of Lists to float

Time:11-30

This is in reference to my previous 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.

CodePudding user response:

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)

CodePudding user response:

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.

  • Related