我是python的新手,我正在做这个练习我必须从文件中的特定行中提取数字。
任务是编写一个提示输入文件名的程序,然后打开该文件并读取文件,查找表单行: X-DSPAM-置信度:0.8475 对这些行进行计数并从每条线中提取浮点值,并计算这些值的平均值并生成输出,如下所示。 您可以在下面测试时在http://www.pythonlearn.com/code/mbox-short.txt下载示例数据,输入mbox-short.txt作为文件名。
到目前为止,我还没有使用代码,我需要得到总数和计算来计算平均值:
http://i.gyazo.com/31373bdbc83c7f421236bc94d323d653.png
我应该得到这个平均结果: 0.750718518519
#Use the file name mbox-short.txt as the file name
fname = raw_input("Enter file name: ")
if len(fname) == 0:
fname = 'file.txt'
fh = open(fname)
count = 0
total = 0
for line in fh:
line = line.rstrip()
if not line.startswith("For example 0.6545") : continue
x = line[20:28]
xx = float(x)
count = count + 1
print xx
#average = total/count
#print average
print "Done"
答案 0 :(得分:2)
尝试一下: 从行中分离出float值后,如图所示列出它们,并使用for循环将这些值相加并计算平均值。 代码:
fname = input("Enter file name:")
fh = open(fname)
count =0
number=[]
total=0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):
continue
#converting string to float
a=float(line[21:])
#adding all the float values to a list
number.append(a)
count =count+1
total=0
#adding all the float values using a for loop
for a in number:
total+=a
avg = total/count
#to check if the numbers have been listed
print(number)
print(count)
print(total)
print("Average spam confidence:",avg)
答案 1 :(得分:0)
试试这个:
# Using Regular Expressions
from re import match
fname = raw_input("Enter file name: ")
if len(fname) == 0:
fname = 'file.txt'
fh = open(fname)
count = 0
total = 0.0
for line in fh:
# Regular expression looks for "For example", followed by whitespace,
# followed by digits (1 or more), with a decimal point, followed by digits (1 or more)
# with some extra possible characters for whatever else (ignored)
m = match('For example\s+(\d+\.\d+).*', line)
if m: # If there was a match, m will have an instance (rather than None)
# m.group(1) holds the float number string
total += float(m.group(1))
count += 1
fh.close()
print "Count:",count
print "Total:",total
print "Average:", total/count
答案 2 :(得分:0)
编辑:哦,现在您已经在评论中posted the file了,并解释说您正在搜索数字,以" X-DSPAM开头的行尾-Confidence:"你要做的事情要清楚得多。这应该做你想要的。同样,一个避免正则表达式的简单解决方案。
答案是在python3中,但如果你正在使用python2,只需切换"输入" to" raw_input"并从print语句中删除括号。
file_name = input("Enter file name: ") # If you're using python2, replace "input" with "raw_input"
numbers = []
# Use with instead of manually opening and closing files. It's safer.
with open(file_name, encoding = "utf-8") as f:
for line in f:
if line.startswith("X-DSPAM-Confidence:"):
# If the number is always the last thing in the line, we can split the
# line on whitespace and grab the last item from the resulting list.
# Then convert the string to a float and append it to our list of
# numbers.
number = float(line.split()[-1])
numbers.append(number)
# Get the average by dividing the sum of the numbers by the length of the list.
average = sum(numbers) / len(numbers)
print(average) # If you're using python2, get rid of the parenthesis.
如果您对列表推导感到满意,那就更简单了:
file_name = input("Enter file name: ") # If you're using python2, replace "input" with "raw_input"
with open(file_name, encoding = "utf-8") as f:
numbers = [float(line.split()[-1]) for line in f if line.startswith("X-DSPAM-Confidence:")]
average = sum(numbers) / len(numbers)
print(average) # If you're using python2, get rid of the parenthesis.
答案 3 :(得分:0)
fname = raw_input("Enter File Name:")
count = 0
a = 0
try:
fh = open(fname)
except:
print 'file doesnt exist:', fname
exit()
for line in fh:
if not line.startswith("X-DSPAM-Confidence:"):continue
print line
colpos = line.find(':')
number = float(line[colpos+1:])
count = count + 1
print number
a = a + number
average = a / count
print 'average:',average
答案 4 :(得分:0)
希望这段代码只需使用解析运算符
即可解决您的问题U仍然可以使用find()找到“:”的位置,并相应地更改切片运算符。
count = 0
total = 0`enter code here`
fname = raw_input("Enter file name: ")
fh = open(fname)
for line in fh:
if line.startswith("X-DSPAM-Confidence:"):
count = count+1
total = total+float(line[20:26])
print "Average spam confidence:",total/count
答案 5 :(得分:0)
你应该试试这个:
fh = open("mbox-short.txt",'r')
count=0
total=0
result=0
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") :continue
count=count+1
num=float(line[21:])
total=num+total
result=total/count
print("Average spam confidence:",result)
这是逻辑。了解三个变量num
,count
,total
和result
。它只是一个简单的代码!
答案 6 :(得分:0)
cnt=0
total=0
final=0
fname = input("Enter file name: ")
fh = open(fname)
for line in fh:
if not line.startswith("X-DSPAM-Confidence:") :
continue
cnt=cnt+1
#print(line.strip())
sm=float(line.split(":")[1])
#print(sm)
total=total+sm
final=total/cnt
print("Average spam confidence:",final)