title: Why is Python showing 'ValueError: could not convert string to float'?
body: <p>I have a CSV containing numbers which I am trying to convert to floats.</p>
  
  <pre><code>filename = "filename.csv"
  enclosed_folder = "path/to/Folder"
  full_path = os.path.join(enclosed_folder,filename)
  
  with open(full_path) as input_data:
      temp = input_data.readlines()
      n = len(temp) #int(temp.pop(0))
      matrix = [x.split(" ") for x in temp]
      for i in range(n):
          for j in range(n):
              matrix[i][j] = float(matrix[i][j])
      input_data.close()
  </code></pre>
  
  <p>When I open the file in any text editor, it does not show the <code>\n</code> at the end of each row.</p>
  
  <p><a href="https://i.stack.imgur.com/S4vVT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S4vVT.png" alt="enter image description here"></a></p>
  
  <p>But running the python code shows the `ValueError: could not convert string to float' because of '\n' being present at the end of each row.</p>
  
  <pre><code>Traceback (most recent call last):
    File "hierarchical-clustering.py", line 37, in &lt;module&gt;
      matrix[i][j] = float(matrix[i][j])
  ValueError: could not convert string to float: '1,0.058824,0.076923,0.066667,0.055556,0.058824,0.071429,0.052632,0.076923,0.0625,0.0625,0.055556,0.055556,0.05,0.066667,0,0,0.055556,0.0625,0.058824,0.058824,0.047619,0.055556,0.0625,0,0.052632,0.066667,0.055556,0.0625,0.058824,0.041667,0.066667,0.058824,0.071429,0.066667,0.076923,0,0.083333,0.052632,0.071429,0.076923,0,0.0625,0.076923,0.058824,0.076923,0.055556,0,0.0625,0.071429,0.0625,0.0625,0.083333,0,0,0,0.058824,0.0625,0,0.058824,0.0625,0.0625,0.066667,0.0625,0.052632,0.066667,0.076923,0.058824,0.071429,0.066667,0.058824,0.071429,0.058824,0.071429,0.058824,0.071429,0.071429\n'
  </code></pre>
  
  <p>So, how do I fix this error?</p>
  
  <p><strong>EDIT:</strong> I used <code>strip()</code> as well as <code>rstrip()</code> as suggested in some of the answers to remove whitespaces, but still the error does not go away:</p>
  
  <pre><code>Traceback (most recent call last):
    File "hierarchical-clustering.py", line 37, in &lt;module&gt;
      matrix[i][j] = float(matrix[i][j].rstrip())
  ValueError: could not convert string to float: '1,0.058824,0.076923,0.066667,0.055556,0.058824,0.071429,0.052632,0.076923,0.0625,0.0625,0.055556,0.055556,0.05,0.066667,0,0,0.055556,0.0625,0.058824,0.058824,0.047619,0.055556,0.0625,0,0.052632,0.066667,0.055556,0.0625,0.058824,0.041667,0.066667,0.058824,0.071429,0.066667,0.076923,0,0.083333,0.052632,0.071429,0.076923,0,0.0625,0.076923,0.058824,0.076923,0.055556,0,0.0625,0.071429,0.0625,0.0625,0.083333,0,0,0,0.058824,0.0625,0,0.058824,0.0625,0.0625,0.066667,0.0625,0.052632,0.066667,0.076923,0.058824,0.071429,0.066667,0.058824,0.071429,0.058824,0.071429,0.058824,0.071429,0.071429'
  </code></pre>