2017-01-06 1 views
-1

안녕하세요, 저는 특정 목록 항목 내에서 문자열에서 숫자 만 추출하지만 결과는 대괄호 []로 인쇄됩니다. 그들을 제거하는 방법? Juanpa이 제안 임 점점 결과는 [2000]입니다 대신파이썬 목록 내에서 문자열의 숫자 값만 추출합니다.

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 

#opening txt file to extract data 
import io 

with io.open("testmodi.txt", "r", encoding="cp1250") as file: 
    ocr_results = [line.strip() for line in file] 

#spliting into list  
for line in ocr_results: 
    print("[" + line + "]") 



class overeview_mth: 
def __init__(self): 
    self.closest_string = "Opis" 
    self.distance_between_closest_string_and_matching_index = + 1 
    self.every_matching_index_list_within_ocr_results = [i for i, x in enumerate(ocr_results) if x == self.closest_string] 
    self.number_of_string_occurence_within_ocr_results = len(self.every_matching_index_list_within_ocr_results) 
    self.matching_index = int(self.every_matching_index_list_within_ocr_results[self.number_of_string_occurence_within_ocr_results -1] + self.distance_between_closest_string_and_matching_index) 



#for testing 
target_index = overeview_mth() 

print([int(s) for s in ocr_results[(target_index.matching_index)].split() if s.isdigit()]) 
+4

목록을 인쇄하고 있으므로 무엇을 기대합니까? 그것에 색인을 붙이면 되겠습니까? –

답변

0

별도로 인쇄 목록의 각 값을 원하는 경우에 2000 년, 당신은 목록에서 그들을 걸릴해야합니다. 코드는 다음과 같습니다.

for s in ocr_results[(target_index.matching_index)].split(): 
    if s.isdigit: 
     print (int(s)) 

그게 효과가 있습니까? 원래 인쇄은 모든 결과를 목록에 넣기 위해 멋진 작업을했지만 실제로 궁극적으로 필요한 것 같지 않습니다.

+0

네, 고마워요!. 프로그래머는 업무용으로 단순한 프로그램이 아닙니다. – euranoo

관련 문제