Skip to content
Advertisement

Trying to find averages from a .txt but I keep getting ValueError: could not convert string to float: ”

I’m using the txt file: https://drive.google.com/file/d/1-VrWf7aqiqvnshVQ964zYsqaqRkcUoL1/view?usp=sharin

I’m running the script:

data = f.read()

ny_sum=0
ny_count=0
sf_sum=0
sf_count=0

for line in data.split('n'):
    print(line)
    parts = line.split('t')
    city = parts[2]
    amount = float(parts[4])

if city == 'San Francisco':
    sf_sum = sf_sum + amount

elif city == 'New York':
    ny_sum = ny_sum + amount
    ny_count = ny_count + 1

ny_avg = ny_sum / ny_count
sf_avg = sf_sum / sf_count

#print(ny_avg, sf_avg)

f = open('result_file.txt', 'w')
f.write('The average transaction amount based on {} transactions in New York is {}n'.format(ny_count, ny_avg))
f.write('The average transaction amount based on {} transactions in San Francisco is {}n'.format(sf_count, sf_avg))

if ny_avg>sf_avg:
    f.write('New York has higher average transaction amount than San Franciscon')
else:
    f.write('San Francisco has higher average transaction amount than New Yorkn')
f.close()

And I ALWAYS get the error: ValueError: could not convert string to float: ”

I’m pretty new-ish to Python and I’m really not sure what I’m doing wrong here. I’m trying to get averages for New York and San Francisco, then export the results AND the comparison to a txt results file

Advertisement

Answer

This should give you what you’re looking for:

from collections import defaultdict as DD

with open('New Purchases.txt') as pfile:
    sums = DD(lambda: [0.0, 0])
    for line in [line.split('t') for line in pfile]:
        try:
            k = line[2]
            sums[k][0] += float(line[4])
            sums[k][1] += 1
        except Exception:
            pass
    for k in ['San Francisco', 'New York']:
        v = sums.get(k, [0.0, 1])
        print(f'Average for {k} = ${v[0]/v[1]:.2f}')
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement