Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python使用字符串和数字从.txt文件计算平均值_Python_String_Parsing_Numbers_Average - Fatal编程技术网

Python使用字符串和数字从.txt文件计算平均值

Python使用字符串和数字从.txt文件计算平均值,python,string,parsing,numbers,average,Python,String,Parsing,Numbers,Average,我是python新手,需要一些关于如何编写代码的帮助 我有一个文本文件,我需要做一些计算。我需要打印出URL1-->平均值(数字+数字+数字)URL2-->平均值等等 输入文本文件: URL1 数 数 数 URL2 数 数 数 URL3 数 数 数 我想要的是 Url1号码 url2号码 url3 numberaverage我就是这样做的,我想以a 2 X X的列格式打印词典,而不是一行打印词典 d=dict() with open('mathtest.txt') as f:

我是python新手,需要一些关于如何编写代码的帮助

我有一个文本文件,我需要做一些计算。我需要打印出
URL1-->平均值(数字+数字+数字)
URL2-->平均值
等等 输入文本文件:

URL1

URL2

URL3


我想要的是

Url1号码

url2号码


url3 numberaverage

我就是这样做的,我想以a 2 X X的列格式打印词典,而不是一行打印词典

    d=dict()
    with open('mathtest.txt') as f:
            while True:
                    url = f.readline().strip()
                    if not url: break
                    average = sum(int(f.readline().strip()) for i in range(3))/3
                    d[url] = average
                    score = str(d[url])
                    print url + " " + score
结果

URL1 2

URL2 5

URL38


URL4 20

我不完全理解这个问题,你能进一步解释一下吗?如果可能的话,还有一个你需要解析的文本文件的例子。你是否尝试过需要帮助,或者正在尝试开始?谢谢!!!!!!我只需要稍微改变一下就可以得到我想要的结果