0

I have a csv file like this (name,floatNumber):

quirrell,0,000281885
flamel,0,000175286
quirrells,0,000154252

I would like to get from it all the float numbers.

for filename in os.listdir('output'):
    with open("output/"+filename, 'rt') as csvfile:
        readCSV = csv.reader(csvfile, delimiter=',')
        for row in readCSV:
            print(row[0],row[1],row[2])

Using delimiter=',', I have this output

quirrell 0 000281885
flamel 0 000175286
quirrells 0 000154252

where the number is splitted. How can I get this output and put all the float inside some variables?

quirrell, 0,000281885
flamel 0,000175286
quirrells 0,000154252

2 Answers 2

1

In this case, you might find it easier not to use the CSV library:

with open('input.csv', 'r') as f_input:
    for row in f_input:
        name, value = row.strip().split(',', 1)
        value = float(value.replace(',', '.'))
        print(name, value)

This would display:

quirrell 0.000281885
flamel 0.000175286
quirrells 0.000154252
Sign up to request clarification or add additional context in comments.

Comments

1

You can convert it to a float after joining the string objects

Ex:

print(row[0],float(".".join([row[1],row[2]])))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.