저는 현재 Python에서 다음과 같은 매우 큰 파일로 작업하고 있습니다.
junk
junk
junk
--- intermediate:
1489 pi0 111 [686] (1491,1492)
0.534 -0.050 -0.468 0.724 0.135
1499 pi0 111 [690] (1501,1502)
-1.131 0.503 12.751 12.812 0.135
--- final:
32 e- 11 [7]
9.072 20.492 499.225 499.727 0.001
33 e+ -11 [6]
-11.317 -17.699 2632.568 2632.652 0.001
12 s 3 [10] (91) >43 {+5}
2.946 0.315 94.111 94.159 0.500
14 g 21 [11] (60,61) 34>>16 {+7,-6}
-0.728 3.329 5.932 6.907 0.950
------------------------------------------------------------------------------
junk
junk
--- intermediate:
repeat
나는 "----------------"줄까지 "--- final"줄 뒤의 모든 두 줄을 결합하고 싶다. 예를 들어, 출력 파일을 읽고 싶습니다.
32 e- 11 [7] 9.072 20.492 499.225 499.727 0.001
33 e+ -11 [6] -11.317 -17.699 2632.568 2632.652 0.001
12 s 3 [10] 2.946 0.315 94.111 94.159 0.500
14 g 21 [11] -0.728 3.329 5.932 6.907 0.950
공백없이 줄에서 추가 항목을 생략하는 방법에 유의하십시오. 내 현재 접근 방식은
start = False
for line in myfile:
line = line.strip()
fields = line.split()
if len(fields)==0:
continue
if not start:
if fields[0] == "----final:":
start = True
continue
len (fields) == 0은 "---------"줄에서 스크립트를 끝내고 다른 "---- final"줄을 볼 때까지 계속해야합니다. 내가 현재 어떻게해야할지 모르겠는 것은 공백이없는 줄의 추가 정보를 생략하면서 두 줄을 함께 결합하는 것입니다. 어떤 제안?
원하는 섹션을 둘러싼 정확한 줄을 알고있는 한 :
#split the large text into lines
lines = large_text.split('\n')
#get the indexes of the beginning and end of your target section
idx_start = lines.index("--- final:")
idx_finish= lines.index("------------------------------------------------------------------------------")
#iterate through the section in steps of 2, split on spaces, remove empty strings, print them as tab delimited
for idx in range( idx_start+1, idx_finish, 2):
out = list(filter(None,(lines[idx]+lines[idx+1]).split(" ")))
print("\t".join(out))
large_text
거대한 문자열로 가져온 파일은 어디에 있습니까 ?
편집 `large_text.txt '파일을 문자열로 열려면 다음을 시도하십시오.
with open('large_text.txt','r') as f:
#split the large text into lines
lines = f.readlines()
#get the indexes of the beginning and end of your target section
idx_start = lines.index("--- final:")
idx_finish= lines.index("------------------------------------------------------------------------------")
#iterate through the section in steps of 2, split on spaces, remove empty strings, print them as tab delimited
for idx in range( idx_start+1, idx_finish, 2):
out = list(filter(None,(lines[idx]+lines[idx+1]).split(" ")))
print("\t".join(out))
가정
split(" ")
에split("\t")
승자가되어야 함 한 줄의 서식 수정 사항이 추가되었습니다. 동일한 가정이 적용됩니다.
with open('./large_text.txt','r') as f:
#split the large text into lines
lines = f.read().split("\n")
#get the indexes of the beginning and end of your target section
idx_start = lines.index("--- final:")
idx_finish= lines.index("------------------------------------------------------------------------------")
#iterate through the section in steps of 2, split on spaces, remove empty strings, print them as tab delimited
for idx in range( idx_start+1, idx_finish, 2):
line_spaces = list(filter(None,lines[idx].split(" ")))[0:4]
other_line = list(filter(None,(lines[idx+1]).split(" ")))
out = line_spaces + other_line
print("\t".join(out))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다