如何在Python中格式化输出? [英] How to format an output in Python?
问题描述
我的代码在这里:
keys = [ '(自相关指数):(\ d + \。?\ d *)','(自相关(\ d + \。?\d *)','(半变异) $ b $ string =''.join(open(dummy.txt)。readlines())
found = []
for key in:
found.extend(re.findall (key,string1))
找到结果:
print'%s =%s'%(result [0],result [1])$ b $ b raw_input()
到目前为止,我得到这个输出:
lockquote
Lag = 1
Lag = 2
Lag = 3
自相关索引= #value
......
......
半变异= #value
期望的输出I w ant是:
$ b $ pre $ 滞后自动相关索引自动相关索引半变量
1 #value #value #value
2#值#value #value
3 #value #value #value
如果这个输出可以可能在 CSV 文件或txt文件,这将是伟大的!
我认为这是一种应该如何输出循环的方法,但是循环并不是很好。
代码(旧版本)基于@mutzmatron回答
键= {'(Lag)=(\ d + \。?\ d *)',
'(自相关索引):(\ d + \。?\ d *)',
'(Semivariance):(\d + \。?\d *)']
import re
string1 = open(dummy.txt)。readlines ).join()
found = []
for keys in key:
found.extend(re.findall(key,string1))
raw_input()
找到结果:
print'%s =%s'%(result [0],result [1])$ b
$ b raw_input()
尚未编译!我使用的是IDLE python 2.6,不知道错误信息,因为我不知道提示符中的暂停命令!
原始问题
我对python完全陌生,有一个问题。我正在尝试处理一个大的文本文件。
这里只是它的一个片段:
Band:WDRVI20((0.2 * b4-b3)/((0.2 * b4)+ b3))
基本统计:
最小值:-0.963805
最大值:0.658219
平均值:0.094306
标准偏差:0.131797
空间统计,***滞后= 1 ***:
观察总数(像素):769995
邻接对的数目:1538146
Moran's I:
***自相关指数:0.8482564597 ***
预期值,如果波段不相关:-0.000001
预期值的标准偏差(标准化):0.000806
预期值的标准偏差(随机):0.000806
Z显着性检验(正常化):1052.029088
Z显着性检验(随机):1052.034915
Geary's C:
***自相关指数:0.1517324729 ***
期望值,如果波段不相关:1.000000
标准偏差的预期值(标准化):0.000807
标准Devi预期值(随机):0.000809
Z显着性检验(标准化):1051.414163
Z显着性检验(随机):1048.752451
***半变异:0.0026356529 ***
空间统计,滞后= 2:
观测总数(像素):769995
相邻对数:3068924
Moran's I:
自相关指数:0.6230691635
预期价值,如果波段是不相关的:-0.000001
期望值的标准偏差(标准化):0.000571
期望值的标准差(随机化):0.000571
Z显着性检验(标准化):1091.521976
Z显着性检验(随机):1091.528022
Geary's C:
自相关指数:0.3769372504
期望值,如果波段不相关:1.000000
标准期望值偏差):0.000574
期望值的标准偏差(随机):0.000587
Z显着性检验(标准化):1085.700399
ZS (随机):1061.931158
Semivariance:0.0065475488
我需要提取信息(例如: Autocorrelation Index
, Semivariance
values)之间进行处理,也许写入一个不同的文本文件或excel文件。我可以这样做吗?帮助将非常感激。
为了按部分格式化数据可能是最简单的工作如下
keys = ['(Lag)=(\ d + \。?\ d *)',
pre>
'(自相关指数):(\ d + \。?\ d *)',
'(半变异):(\ d + \。?\ d *)']
import re
string1 =''.join(open(dummy.txt)。readlines())
sections = string1.split('Spatial Statistics')
输出= []
头= []
在枚举(部分)中:秒
找到= []
输出。
found.extend(re.findall(key,sec))
找到结果:
print'%s =% (结果[1])$ b $ b如果len(找到)>结果[1])$ b $ b输出[-1] .append 0& len(heads)== 0:
heads = [result [0] for found found]
fout = open('output.csv','w')
wrt = csv.writer(fout)
wrt.writerow(heads)
wrt.writerows(outputs)
fout.close()
I am having difficulty in formatting some code in Python: My code is here:
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()
So far, I am getting this output:
Lag = 1
Lag = 2
Lag = 3
Autocorrelation Index = #value
......
......
Semivariance = #value
But the desired output I want is:
Lag AutoCorrelation Index AutoCorrelation Index Semivariance 1 #value #value #value 2 #value #value #value 3 #value #value #value
If this output can be possible in a CSV file or a txt file, that would be great!
I think this is a way how you should output the loops, but I am not that great with loops.
My updated code (OLD version)
based on @mutzmatron answer
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()
not yet compiling! I am using IDLE python 2.6 , don't know the error messages since I don't know the pause command in the prompt!
Original Question
I am totally new to python and have a question. I am trying to process a large text file. Here is just a snippet of it:
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
I need to extract the information in between the star *** values ( eg :
Autocorrelation Index
,Semivariance
values ) and process it , maybe write it to a different text file or excel file. Can I do that? Help would be really appreciated.解决方案In order to format the data by section perhaps it's easiest to work on the segments as follows
keys =['(Lag)=(\d+\.?\d*)', '(Autocorrelation Index): (\d+\.?\d*)', '(Semivariance): (\d+\.?\d*)'] import re string1 = ''.join(open("dummy.txt").readlines()) sections = string1.split('Spatial Statistics') output = [] heads = [] for isec, sec in enumerate(sections): found = [] output.append([]) for key in keys: found.extend(re.findall(key, sec)) for result in found: print '%s = %s' % (result[0],result[1]) output[-1].append(result[1]) if len(found) > 0 & len(heads) == 0: heads = [result[0] for result in found] fout = open('output.csv', 'w') wrt = csv.writer(fout) wrt.writerow(heads) wrt.writerows(outputs) fout.close()
这篇关于如何在Python中格式化输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!