3

I'm trying to convert a string list into floats but this can't be done with a number like 1,234.56. Is there a way to use the string.replace() function to remove the comma so i just have 1234.56? string.replace(',','') doesn't seem to work. This is my current code:

fileName = (input("Enter the name of a file to count: "))
print()

infile = open(fileName, "r")
line = infile.read()
split = line.split()
for word in split:
    if word >= ".0":
        if word <= "9":
            add = (word.split())
            for num in add:
                  x = float(num)
                  print(x)

This is my error I'm getting:

File "countFile.py", line 29, in main x = float(num) ValueError: could not convert string to float: '3,236.789'

user3330472
  • 89
  • 2
  • 10

2 Answers2

6

On a string you can replace any character, such as ,, like so:

s = "Hi, I'm a string"
s_new = s.replace(",", "")

Also, the comparisons you are doing on the strings may not always perform the way you expect. It may be better to cast to numeric values first. Something like:

for word in split:
    n = float(word.replace(",", ""))
    # do comparison on n, like
    # if n >= 0: ...

As a tip, try reading in your file with with:

# ...
with open(fileName, 'r') as f:
    for line in f:
        # this will give you `line` as a string 
        # ending in '\n' (if it there is an endline)
        string_wo_commas = line.replace(",", "")
        # Do more stuff to the string, like cast to float and comparisons...

This is a more idiomatic way to read in a file and do something to each line.

bnjmn
  • 4,508
  • 4
  • 37
  • 52
  • I've tried adding this in various parts of my code and i get this error: AttributeError: 'list' object has no attribute 'replace' – user3330472 Feb 20 '14 at 03:08
  • `replace` is a string method. It can only be used on a string, not a list of strings (or list of anything else for that matter). I've updated my answer with an example of how to read each line of the file in as a string and do something to it a bit more easily. Hopefully that helps. – bnjmn Feb 20 '14 at 03:21
2

Check out this: How do I use Python to convert a string to a number if it has commas in it as thousands separators? and this: How to delete a character from a string using python?

Also, note that your word >= ".0" comparisons are string comparisons, not numerical. They may not do what you think they will. For example:

>>> a = '1,250'
>>> b = '975'
>>> a > b
False
Community
  • 1
  • 1
rpmartz
  • 3,759
  • 2
  • 26
  • 36
  • I'm pulling the numbers from a text file. Is there a better way to pull only the numbers without getting the words between them? – user3330472 Feb 20 '14 at 03:12
  • That's hard to answer without knowing anything about the format of the text and numbers in the file, but you could use a regular expression. For example, you could tokenize the text and then choose to process anything that matches the regular expression `[0-9]+,?[0-9]+\.[0-9]+` or something. You could simplify it and process any token that starts with `[0-9]` (look up `search()` and `match()` in the Python `re` module) – rpmartz Feb 20 '14 at 03:19