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

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

我正在使用 txt 文件:https://drive.google.com/file/d/1-VrWf7aqiqvnshVQ964zYsqaqRkcUoL1/view?usp=sharin

我是运行剧本:

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 Francisco\n')
else:
    f.write('San Francisco has higher average transaction amount than New York\n')
f.close()

而且我总是收到错误消息: ValueError:无法将字符串转换为浮点数:''

我对 Python 还很陌生,我真的不确定我做错了什么。我正在尝试获取纽约和旧金山的平均值,然后将结果和比较结果导出到 txt 结果文件

我重新整理了代码。我同意 BrutusFocus 的观点,即拆分使得很难准确读取每一行的位置。我已对其进行设置,因此如果它在行中的任何一点看到该位置,它都会计算在内。

with open("data.txt", "r") as f:
    data = f.read()

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

for line in data.split('\n'):
    parts = line.split('\t')
    city = parts[2]
    amount = float(parts[4])
    print(city, amount)
    if "New York" in line:
        ny_sum = ny_sum + amount
        ny_count = ny_count + 1
    elif "San Francisco" in line:
        sf_sum = sf_sum + amount
        sf_count = sf_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 Francisco\n')
else:
    f.write('San Francisco has higher average transaction amount than New York\n')
f.close()

这应该能满足您的需求:

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}')