问题描述
早上好,我是python新用户,我对MATLAB有一些经验。为了练习,我想写一个脚本,这对我的工作也有帮助。我有一个大文件,我想提取一些列并排放置(这些列的长度相同)。 所以,我从这样的情况开始 :
Great amount of text and numbers
1 2 3 4 5
O O O O O
SpecialText -- text text text text text
1 1 A 1A col1 col2 col3 col4 col5
2 2A col1 col2 col3 col4 col5
3 2BX col1 col2 col3 col4 col5
4 2BY col1 col2 col3 col4 col5
5 2BZ col1 col2 col3 col4 col5
6 2 B 3A col1 col2 col3 col4 col5
7 3AX col1 col2 col3 col4 col5
8 3AY col1 col2 col3 col4 col5
6 7 8 9 10
O O O O O
SpecialText -- text text text text text
1 1 A 1A col6 col7 col8 col9 col10
2 2A col6 col7 col8 col9 col10
3 2BX col6 col7 col8 col9 col10
4 2BY col6 col7 col8 col9 col10
5 2BZ col6 col7 col8 col9 col10
6 2 B 3A col6 col7 col8 col9 col10
7 3AX col6 col7 col8 col9 col10
8 3AY col6 col7 col8 col9 col10
我想获得类似的东西
col1 col2 col3 col4 col5 col6 col7 ...
col1 col2 col3 col4 col5 col6 col7 ...
col1 col2 col3 col4 col5 col6 col7 ...
col1 col2 col3 col4 col5 col6 col7 ...
... ... ... ... ... ... ... ...
我的问题很简单。这对初学者可行吗?我可以使用任何库来简化这项工作吗? 很抱歉,我的经验不足,谢谢。
编辑:起始文件是.txt文件
EDIT2:为清楚起见修改了起始文件
解决方法
请使用以下程序
$('#response').on('keyup',function(e) {
// if response is not set return false
if ($("#response").length && !$.trim($("#response").val())){
$('#valid').attr('disabled',true)
}else {
// if response is set and inputs are not disabled,return true
if($('#first,#second,#third').is(':disabled')){
$('#valid').attr('disabled',false)
}
}
});
下面将显示给定的txt文件。
import re
result={}
with open("input.txt","r") as f:
insidedatablock=False
line=f.readline()
while line!='':
if line.strip().startswith("SpecialText"):
insidedatablock=True
if line.strip()=='':
insidedatablock=False
if insidedatablock==True:
cols=re.split("\s+",line.strip())
if cols[0].isdigit() and len(cols)>=7:
if cols[0] in result:
result[cols[0]]+=cols[-5:]
else:
result[cols[0]]=cols[-5:]
line=f.readline()
for i in result:
print(" ".join(result[i]))