tags:

views:

262

answers:

2

Hello,

I am trying to compare two lists of string in python. Some of the strings are numbers however I don't want to use it as number, only for string comparison.

I read the string from a file and put them on a list like this:

def main():
    inputFileName = 'BateCarteira.csv'
    inputFile = open(inputFileName, "r")

    bankNumbers = []

    for line in inputFile:
        values = line[0:len(line)-1].split(';');

        if (len(values[0]) > 3):
            bankNumbers.append(''+values[0])

However, when I try to print the number, it prints like:

1,20091E+11

The code for the printing:

    print 'not in the list: ' + bankNumber
    outputFile.write(bankNumber + '-')

What can I do so python never casts the string to an int?

sorry for my english :D

+2  A: 

Python never transforms a string to a number, unless you try something like:

s = "1.2"
a = float(s)

So I guess that your .csv file has the string "1,20091E+11" inside it. Also notice that the decimal point is a coma. So, if you tried to convert it to a float, the transformation would fail.

cyberthanasis
Sounds like this is right. I suspect the input file has unexpected values. Some locales like German and French use , for . in numbers.
UberAlex
You may want to look at the standard "locale" library module.
Kevin Little
Yeah, that was right...the atof function also helps.the language is portuguese.
Túlio Caraciolo
+1  A: 

You need the locale module to read numbers in the locale format (i.e with decimal comma that's used in (most) parts of Europe).

import locale
locale.setlocale(locale.LC_ALL, '')
f = locale.atof("1,20091E+11")
kaizer.se