2017-01-06 11 views
-1

こんにちは、特定のリスト項目内の文字列から数値を抽出するだけですが、結果は角カッコ[]で印刷されます。それらを削除するには?イムは、取得 結果ではなく、2000年Pythonリスト内の文字列から数値のみを抽出する

#!/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]は、あなたが個別に印刷されたリストの各値をしたい場合Juanpaが示唆したように、あなたは、リストからそれらを取る必要があります。コードは次のようなものになります。

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

これは機能しますか?あなたのオリジナルのプリントは、すべての結果をリストに入れるには素敵な余計な作業をしましたが、最終的に必要なものではありません。

+0

はいありがとうthats it !.私はプログラマーではなく、仕事のための単なる単純なプログラムです。 – euranoo

関連する問題