2016-04-13 10 views
-1



私はimdbpieモジュールを使ってImdbに問い合わせ、ムービータイトルを取得しています。 Python - 長い出力文字列をどのようにフォーマットできますか?

from imdbpie import Imdb 
imdb = Imdb() 
film = str(imdb.search_for_title('some_title')) 
tit = re.sub(r'[^\w]|year|title|imdb_id|tt[0-9]{7}', ' ', film) 
print(tit) 

私は不要なパターンを剥離し、出力を得る:

2015      The Merchant Gaekju 2015   2015      Murderers Mobsters Madmen Vol 2 Assassination in the 20th Century   1993      The Manzai 2015 Pre Masters   2015      The 2015 World Series   2015      2015 Foster Farms Bowl   2015      2015 Nephilim Monsters Giants Conference   2015      2015 The Disaster Diaries 2015 L Agenda Des Cataclysmes   2015      The Lobster   2015      Brooklyn Lobster   2005      The Oscar Nominated Short Films 2015 Animation   2015      The Oscar Nominated Short Films 2015 Live Action   2015      La langosta azul   1954      The Fresh Lobster   1948      The Lobster   2013      The Oscar Nominated Short Films 2015 Documentary   2015      A Visit to a Crab and Lobster Factory   1913      BBC Election Debate 2015 The Reaction   2015      Easter Bowl 2011 Beneath the Surface   2011      The Lonesome Lobster   2010 

文字列のラインで、ランダムVARS「年」と「ムービータイトル」が含まれています。 私はこのような形式この出力たい:

2015ザ・マーチャントGaekju 2015
2015殺人20世紀における2暗殺巻青春の群像狂人
2015 2015ワールドシリーズ
2015フォスター農場ボウル
2015ネフィリムモンスターをジャイアンツ会議
2015ザ・災害日記
2015 LアジェンダデCataclysmes
... ... ...

コードを少し変更して、出力文字列に改行文字を追加して、基本的には必要なものを手に入れましたが、他にもっとエレガントな方法があります。

tit = re.sub(r'[^\w'+rlist+']|year|title|imdb_id|tt[0-9]{7}', ' ', film) 
ntit = re.sub(r'}', '\n', tits) 
f = open('titles.txt', 'wt') 
print(ntit, file=f) 
f.close() 

$猫titles.txt
国民の宝くじスター2015 2015
20世紀1993
漫才2015プリマスターズ2015
2015年のワールドシリーズでの商人Gaekju 2015 2015
殺人青春の群像狂人集2暗殺2015
フォスターファームズボウル2015

+0

希望の出力の最初の行にも1年の終わりがあるのはなぜですか? – wnnmaw

+0

正規表現を使用しようとしましたか? – th3an0maly

+0

私には...これはメソッドや関数を開発するための規則的なパターンを持っていないようですが、私の提案はあなたが投稿したものよりも良い出力を持つように出力関数を生成するように修正しようとしています。 –

答えて

0

これは面倒ですが、パターンがあります。それは年です。その後に21のスペースが続き、その後にタイトルが続き、その後に9つのスペースが続き、それが再び始まります。証明:

>>> import re 
>>> map(len, re.findall(r'\s{4,}', s)) 
[21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9, 21, 9] 

ただし、これらの正確な数値に頼るのは賢明ではありません。

>>> from pprint import * 
>>> pprint(re.findall(r'(.+?)\s{15,}(.+?)\s{5,}', s)) 
[('2015', 'The Merchant Gaekju 2015'), 
('2015', 
    'Murderers Mobsters Madmen Vol 2 Assassination in the 20th Century'), 
('1993', 'The Manzai 2015 Pre Masters'), 
('2015', 'The 2015 World Series'), 
('2015', '2015 Foster Farms Bowl'), 
('2015', '2015 Nephilim Monsters Giants Conference'), 
('2015', '2015 The Disaster Diaries 2015 L Agenda Des Cataclysmes'), 
('2015', 'The Lobster'), 
('2015', 'Brooklyn Lobster'), 
('2005', 'The Oscar Nominated Short Films 2015 Animation'), 
('2015', 'The Oscar Nominated Short Films 2015 Live Action'), 
('2015', 'La langosta azul'), 
('1954', 'The Fresh Lobster'), 
('1948', 'The Lobster'), 
('2013', 'The Oscar Nominated Short Films 2015 Documentary'), 
('2015', 'A Visit to a Crab and Lobster Factory'), 
('1913', 'BBC Election Debate 2015 The Reaction'), 
('2015', 'Easter Bowl 2011 Beneath the Surface'), 
('2011', 'The Lonesome Lobster')] 

pprintはここでしか出力フォーマットのためです:ちょうどメディアのギャップと交互に大きなギャップがあることを言うと、そのようにそれらをキャプチャしてみましょう。 Hereは、正規表現の説明です。

関連する問題