我有一个包含
的logdata.txt
文件
07/05/2016 11:39 0.00167
07/05/2016 11:39 0.00333
07/05/2016 11:39 0.00167
07/05/2016 11:40 0.00333
07/05/2016 11:40 0.005
07/05/2016 11:40 0.00667
我写了一个代码,它总结了第三列,输出是
07/05/2016 0.02167
此结果存储在senddata.txt
f = open('logdata.txt','r+')
res = OrderedDict()
for line in f:
values = line.split('')
if len(values) == 4:
date = values[0]
val = values[3]
if res.get(date):
res[date] += int(val)
else:
res[date] = int(val)
f.close()
f = open('senddata.txt', 'w')
for line in res.keys():
f.write('{} {}'.format(line, res[line]))
f.close()
出现以下错误
Traceback (most recent call last):
res[date] = int(val)
ValueError: invalid literal for int() with base 10: '0.00167\n'
任何人都可以帮我调试它......
答案 0 :(得分:0)
从最后一个文字中删除“\ n”,以便将其转换为float。
val = values[3].split('\n')[0]
if res.get(date):
res[date] += float(val)
else:
res[date] = float(val)
答案 1 :(得分:0)
问题是# Always use a with statement to open files, so it
# closes automatically even if something goes wrong
# You're not writing to the file so the default mode 'r' is fine
with open('logdata.txt') as logdata:
res = OrderedDict()
for line in logdata:
# Passing no arguments splits on whitespace and removes empty strings
# so the result is intuitive and there's no need to 'clean' it
values = line.split()
# Note that the length is now 3, not 4
if len(values) == 3:
# Tuple unpacking is sometimes much nicer than using an index
# _ is the conventional way of saying 'a value I don't care about'
date, _, val = values
# Much nicer than an if statement
# If .split() hadn't removed the whitespace the best thing to do
# would be to remove it with val.strip() or val.rstrip()
res[date] = res.get(date, 0) + float(val)
# Again, not the with statement
with open('senddata.txt', 'w') as f:
# This is how to get both key and value at once
# Even with your old method, 'for line in res:' is good enough
# as iteration directly over a dict means over its keys
for line, value in res.items():
f.write('{} {}'.format(line, value))
这是换行符,当你逐行遍历文件时,它几乎总是出现在一行的末尾。我已经展示了如何修复它以及如何改进代码的其他方面。
if len(values) == 3
您可能希望删除date, _, val = lines.split()
,因为如果不是这样,则可能表示您想要修复的无效数据,而不是静默地传递它。在这种情况下,您也可以说collections.defaultdict
。
越来越先进,有一个更简洁的方法来处理默认的起始值0.通常你会使用OrderedDict
,但如果你已经需要,这不是一个简单的选择class OrderedFloatDict(OrderedDict):
def __missing__(self, key):
return 0
...
res = OrderedFloatDict()
...
res[date] += float(val)
。因此,您可以手动获得相同的功能:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Your Message: "
android:textAppearance="?android:attr/textAppearanceLarge" />
<View
android:layout_width="150dp"
android:layout_height="1dp"
android:layout_alignBottom="@+id/textView"
android:layout_gravity="bottom"
android:layout_toEndOf="@+id/textView"
android:layout_toRightOf="@+id/textView"
android:background="#FF0000FF" />
</RelativeLayout>