我有在格式化一些Python代码的困难:我的代码是在这里:
keys = ['(Lag)=(\d+\.?\d*)','\t','(Autocorrelation Index): (\d+\.?\d*)', '(Autocorrelation Index): (\d+\.?\d*)', '(Semivariance): (\d+\.?\d*)']
import re
string1 = ''.join(open("dummy.txt").readlines())
found = []
for key in keys:
found.extend(re.findall(key, string1))
for result in found:
print '%s = %s' % (result[0],result[1])
raw_input()
到目前为止,我得到这样的输出:
滞后= 1
滞后= 2
滞后= 3
自相关指数=#VALUE
......
......
半方差=#VALUE
但所需的输出我想要的是:
Lag AutoCorrelation Index AutoCorrelation Index Semivariance
1 #value #value #value
2 #value #value #value
3 #value #value #value
如果此输出可以在可能的CSV文件或txt文件,这将是伟大的!
我觉得这是一种方法,我怎么你应该输出的循环,但我不与循环很大。
我更新的代码(老版)
基于@mutzmatron答案
keys = ['(Lag)=(\d+\.?\d*)',
'(Autocorrelation Index): (\d+\.?\d*)',
'(Semivariance): (\d+\.?\d*)']
import re
string1 = open("dummy.txt").readlines().join()
found = []
for key in keys:
found.extend(re.findall(key, string1))
raw_input()
for result in found:
print '%s = %s' % (result[0], result[1])
raw_input()
尚未编制! 我使用IDLE Python 2.6中,不知道该错误消息,因为我不知道在提示暂停命令!
原来的问题
我完全新的Python,有一个问题。 我想处理大量的文本文件。 这里只是它的片段:
Band: WDRVI20((0.2*b4-b3)/((0.2*b4)+b3))
Basic Statistics:
Min: -0.963805
Max: 0.658219
Mean: 0.094306
Standard Deviation: 0.131797
Spatial Statistics, ***Lag=1***:
Total Number of Observations (Pixels): 769995
Number of Neighboring Pairs: 1538146
Moran's I:
***Autocorrelation Index: 0.8482564597***
Expected Value, if band is uncorrelated: -0.000001
Standard Deviation of Expected Value (Normalized): 0.000806
Standard Deviation of Expected Value (Randomized): 0.000806
Z Significance Test (Normalized): 1052.029088
Z Significance Test (Randomized): 1052.034915
Geary's C:
***Autocorrelation Index: 0.1517324729***
Expected Value, if band is uncorrelated: 1.000000
Standard Deviation of Expected Value (Normalized): 0.000807
Standard Deviation of Expected Value (Randomized): 0.000809
Z Significance Test (Normalized): 1051.414163
Z Significance Test (Randomized): 1048.752451
***Semivariance: 0.0026356529***
Spatial Statistics, Lag=2:
Total Number of Observations (Pixels): 769995
Number of Neighboring Pairs: 3068924
Moran's I:
Autocorrelation Index: 0.6230691635
Expected Value, if band is uncorrelated: -0.000001
Standard Deviation of Expected Value (Normalized): 0.000571
Standard Deviation of Expected Value (Randomized): 0.000571
Z Significance Test (Normalized): 1091.521976
Z Significance Test (Randomized): 1091.528022
Geary's C:
Autocorrelation Index: 0.3769372504
Expected Value, if band is uncorrelated: 1.000000
Standard Deviation of Expected Value (Normalized): 0.000574
Standard Deviation of Expected Value (Randomized): 0.000587
Z Significance Test (Normalized): 1085.700399
Z Significance Test (Randomized): 1061.931158
Semivariance: 0.0065475488
我需要在恒星之间提取信息***值(例如: Autocorrelation Index
, Semivariance
值),并对其进行处理,可能将其写入不同的文本文件或Excel文件。 我能做到这一点? 帮助将非常感激。