-1

Code

    for i in range(num):
           # for j in range(4):
              lines = outf1.readline()
              brr[i]= list(map(float, lines.split()))

This is my input dataset

['1', '1.52101', '13.64', '4.49', '1.1', '71.78', '0.06', '8.75', '0', '0', '1']
['2', '1.51761', '13.89', '3.6', '1.36', '72.73', '0.48', '7.83', '0', '0', '1']
['3', '1.51618', '13.53', '3.55', '1.54', '72.99', '0.39', '7.78', '0', '0', '1']

I am working with SOM algorithm. I have a file containing float values having 4 or 5 decimal values. I need to retrieve these values and store to array.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Pooja
  • 59
  • 3
  • 8
  • 2
    Well, what would you expect `float("['1',")` to evaluate to? When you say that's your input dataset, do you mean that you have lists of strings, or strings that look like lists of strings, or ...? Where did `outf1` come from? – jonrsharpe Nov 06 '14 at 11:20
  • How the data is stored in file ? what is this input dataset you are talking about? as i can see you are not giving any thing as list. – Sravan K Ghantasala Nov 06 '14 at 11:21
  • See the link-->http://archive.ics.uci.edu/ml/machine-learning-databases/glass/glass.data...This is my dataset taken from UCI repository.List is brr[i].I have declared also the list as arr = [[0 for x in range(num)] for x in range(num)] – Pooja Nov 06 '14 at 11:29
  • If you're actually opening that file into `outf1`, you should **not** be getting what you claim to get. Have you added `str` somewhere? Note you should `split(',')` - the elements are separated by commas, not whitespace. Provide a **[minimal example](http://stackoverflow.com/help/mcve) of code** and the **full error traceback**. – jonrsharpe Nov 06 '14 at 11:30
  • I have already tried...arr[i]=map(float, lines.strip().split(','))..Still no change – Pooja Nov 06 '14 at 11:37

2 Answers2

1

ast.literal_eval() should do the trick:

In [12]: line = "['1', '1.52101', '13.64', '4.49', '1.1', '71.78', '0.06', '8.75', '0', '0', '1']"

In [13]: map(float, ast.literal_eval(line))
Out[13]: [1.0, 1.52101, 13.64, 4.49, 1.1, 71.78, 0.06, 8.75, 0.0, 0.0, 1.0]
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

You seem to be feeding split a line that starts with ['1', so of course that square bracked is going to be a problem.

The quick fix is to just strip each line (to make sure there's no whitespace), then remove first and last character to get rid of the brackets.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Thanks for reply! I have tried arr[i]=map(float, lines.strip().split(','))..but still the same error persists – Pooja Nov 06 '14 at 11:26
  • Can u suggest how to get rid of brackets...file contains the datasets with brackets.. – Pooja Nov 06 '14 at 11:39