-1

我正在使用 python33 并想从包含单词和数字的文本文件中获取一串数字(下面复制/粘贴的 txt 文件中的第 30 行)。捕获所需的数字后,我想使用一个等式(即(number-30)/2)转换它们,然后找到它们的平均值并将其写入一个新文件(例如 Scope22.txt 或 csv)。我有许多单独的文件,这是第一个必须进行类似处理的文件,每个平均值都将写入同一个文件(Scope22.txt 或 csv)。附件包含关注的数量(0.0, 66.0, 70.0, 0.0, 0.0, 81.0, 64.0, 0.0, 76.0)......转换它们((number-30)/ 2)然后找到它们的平均值。
我衷心感谢您的帮助。

我的.txt:

次要类型范围
有效时间 201012132300
范围 536
西塔 48
身份证坐
名称 站点
拉特中心 23.45459
伦敦中心 -22.766379
分辨率1 1.0
分辨率1 1.0
高度 1 11.0
高度3 600.0
数据索引
输入文件类型 AAA,0,1,PPTOA
次要产品参数 SCOPe,,BBBBBBB,KKKKK_LIST_SIT,0,0,1,1
TableLabels_Locations ID 名称 有效纬度经度范围 Theta BBN Yyy
TableStart_SiteLocations
"LLL", "QQQQ_WWWWW", 1, 23.45459,-22.766379, 536.11,48.14, 55,22;
TableEnd_SiteLocations
TableLabels_TableInfoSites ID Lat Lon Range Theta BMHT PNTVlU Min Max Avg Median P95 AbsDev Var Sd Skew Curt
TableStart_TableInfoSites
LLL, 23.45459,-22.766379, 536.11,48.14, -1.00, 0.00, 0.00, 81.00, 39.67, 64.00, 0.00, 35.26,1.44e+03, 37.96, -0.14, -2.13;
TableEnd_TableInfoSites
TableLabels_TableDis Dis M
TableStart_TableDis
LLL, 4,200,190,180,170,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,0,0,1,0, 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,;
TableEnd_TableDis
TableLabels_TableData 值_M
TableStart_TableData
LLL, 0.0, 66.0, 70.0, 0.0, 0.0, 81.0, 64.0, 0.0, 76.0,;
TableEnd_TableData

这是我的代码:

STATS11_filename='C:/Python33/STATS11.txt'
STATS11_filename=open(STATS11_filename,'r')
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline() 
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline() 
line=STATS11_filename.readline()
line=STATS11_filename.readline()
line=STATS11_filename.readline()    
line=STATS11_filename.readline()
b=line=STATS11_filename.readline()
d=(str(b[7:-3]))
print(len(b[7:-3]))
STATS11_filename.close()
a=7
Scope22_filename='C:/Python33/Scope22.txt'
Scope22_filename=open(Scope22_filename,'w')
for i in range (len(b[7:-3])):
    if (b[a])>='64' or (b[a])<='254':
        Scope22_filename.write(b[a])       
             if a==53:
                 quit
             else:
                 a=a+2
Scope22_filename.close()
4

1 回答 1

0

您的变量 b 是一个字符串(在您的示例中,b 等于 "0.0, 66.0, 70.0, 0.0, 0.0, 81.0, 64.0, 0.0, 76.")并且您的变量 a 是一个 int (a 等于 7),然后是 b[a]将只引用一个字符,这不能是 '64'(两个字符长)。

我想你想要做的是像这样得到一片if float(b[a:a+3])>=64 or float(b[a:a+3])<=254:

(在 Python 中可以通过 来改进if 64 <= float(b[a:a+3]) <= 254:

你真的想计算这一系列数字的平均值吗?

如果是这样,您应该执行以下操作:

b = list(map(lambda n: (float(n)-30)/2, b.split(","))
b = sum(b)/len(b)

与你的概率无关,但不是复制/过去一堆line=STATS11_filename.readline()你可以做的[STATS11_filename.readline() for i in range(29)](即使我认为必须有一个函数可以跳转到你想要的行(也许))。

编辑:确实,您也可以这样做:b = STATS11_filename.readlines()[29].

于 2013-02-01T02:07:54.120 回答