所以这是我的文本文件,我想计算每年的平均值:
1969 324.000
1970 330.190
1970 326.720
1970 327.130
1971 326.970
1971 331.200
1971 329.430
1971 335.770
1971 337.600
这是我从另一个问题得到的代码,但它仍然存在错误:
In[2]: result = {}
with open('filename.txt', 'r') as f:
for line in f:
year, num = line.split()
year = int(year)
num = float(num)
try:
result[year].append(num)
except KeyError:
result[year] = [num]
In[3]: for k, v in sorted(result.items()):
print('Year: {}\tAverage: {:.2f}'.format(k, sum(v) / len(v)))
我的错误:
Traceback (most recent call last):
File "C:\Users\fra02.sek\Desktop\average.py", line 4, in <module>
year, num = line.split()
ValueError: too many values to unpack (expected 2)
答案 0 :(得分:0)
修正了缩进并添加了空行检查:
for fname in glob.glob(path):
fname = fname.replace(r'\2016', '/2016')
f = pd.DataFrame(pd.read_csv(fname))
f = f.replace({'Hour': {'24:00:00': '00:00'}})
f['Date'] = pd.to_datetime(f['Date']).dt.strftime('%d/%m/%Y')
f['Hour'] = pd.to_datetime(f['Hour']).dt.strftime('%H:%M')
m = f['Hour'] == '00:00'
dates = (pd.to_datetime(f['Date']) + pd.Timedelta(1, unit='d')).dt.strftime('%d/%m/%Y')
f['Date'] = np.where(m, dates, f['Date'])
print(fname)
if a == 0:
f_2016['Date'] = f['Date']
f_2016['Hour'] = f['Hour']
a = 1
f_2016 = pd.merge(f_2016, f, on=['Date', 'Hour'])
print(pd.DataFrame.head(f_2016, n=100))
另一种解决方案,我认为更具可读性:
result = {}
with open('filename.txt', 'r') as f:
for line in f:
if line.strip()!='':
year, num = line.split()
year = int(year)
num = float(num)
try:
result[year].append(num)
except KeyError:
result[year] = [num]
for k, v in sorted(result.items()):
print('Year: {}\tAverage: {:.2f}'.format(k, sum(v) / len(v)))