2017-08-08 14 views
-2

これをどのようにしてPythonで行いますか?別のファイル(Python)に基づいてファイルからフレーズを削除する

badphrases.txtは

Go away 
Don't do that 
Stop it 

allphrases.txt私はallphrases.txtはbadphrases.txtにおけるラインのきれいになりたい

I don't know why you do that. Go away. 
I was wondering what you were doing. 
You seem nice 

が含まれていますが含まれています。

それは

cat badfiles.txt | while read b 
do 
cat allphrases.txt | grep -v "$b" > tmp 
cat tmp > allphrases.txt 
done 

ああ、あなたは私が見たり試していないと思っていたbashで些細です。私は何度も何度も探しました。

ここに私のコードです:

# Files 
ttv = "/tmp/tv.dat" 
tmp = "/tmp/tempfile" 
bad = "/tmp/badshows" 

badfilesすでに右ここにコードがTTV

# Function grep_v 
def grep_v(f,str): 
    file = open(f, "r") 
    for line in file: 
      if line in str: 
       return True 
    return False 

t = open(tmp, 'w') 
tfile = open(ttv, "r") 
for line in tfile: 
    if not grep_v(bad,line): 
      t.write(line) 
tfile.close 
t.close 
os.rename(tmp, ttv) 

答えて

0

まずPythonでファイルを読み込む方法をgoogleの作成...
が存在する:

あなたはおそらくこのようなものになるでしょう:How do I read a file line-by-line into a list?

使用このリスト

with open('badphrases.txt') as f: 
    content = f.readlines() 
badphrases = [x.strip() for x in content] 

with open('allphrases.txt') as f: 
    content = f.readlines() 
allphrases = [x.strip() for x in content] 

内のファイルの両方を読み取るために今、あなたはリスト内のコンテンツの両方を持っています。

すべてのフレーズを繰り返して、その中にバッドフレーズのフレーズが含まれているかどうかを確認します。あなたがグーグルを検討するかもしれないこの時点で

  • リストパイソン
  • を反復処理する方法を別の文字列のpython

に存在する文字列は、それらの場所からコードを取るかどうかを確認する方法このようなブルー​​トフォースアルゴを作った:

for line in allphrases: 
    flag = True 
    for badphrase in badphrases: 
     if badphrase in line: 
      flag = False 
      break 
    if flag: 
     print(line) 

このコードを読んでから、printと出力をファイルに置き換える必要があることに気付くでしょう。

  • Googleはどのようにファイルをpythonに出力するのですか?

次に、アルゴリズムを改善する方法について考えてみましょう。ではごきげんよう。

UPDATE:

@COLDSPEEDを提案することができます簡単なグーグル - Pythonでファイルの行を交換する方法:また、作品Search and replace a line in a file in Python

あなたはこのような何かを得る可能性があります。

+0

Googleに「Pythonでファイル内の行を置き換える方法」と尋ねることもできます。 –

+0

これを行う方法は100通りあります。明らかに、彼はPythonを学ぼうとしています。いくつかの基本的なヒントを与えます。 –

+0

場合によっては、別の言語での操作が簡単になることがあります。 Pythonのソリューションが何であれ、それは必要以上に複雑になるでしょう。私はなぜPythonが普及しているのか分からない。 –

0

解決策はそれほど悪くありません。

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

import feedparser, os, re 

# Files 
h = os.environ['HOME'] 
ttv = h + "/WEB/Shows/tv.dat" 
old = h + "/WEB/Shows/old.dat" 
moo = h + "/WEB/Shows/moo.dat" 
tmp = h + "/WEB/Shows/tempfile" 
bad = h + "/WEB/Shows/badshows" 

# Function not_present 
def not_present(f,str): 
    file = open(f, "r") 
    for line in file: 
      if str in line: 
       return False 
    return True 

# Sources (shortened) 
sources = ['http://predb.me/?cats=tv&rss=1'] 

# Grab all the feeds and put them into ttv and old 
k = open(old, 'a') 
f = open(ttv, 'a') 
for h in sources: 
    d = feedparser.parse(h) 
    for post in d.entries: 
      if not_present(old,post.link): 
       f.write(post.title + "|" + post.link + "\n") 
       k.write(post.title + "|" + post.link + "\n") 
f.close 
k.close 

# Remove shows without [Ss][0-9] and put them in moo 
m = open(moo, 'a') 
t = open(tmp, 'w') 
file = open(ttv, "r") 
for line in file: 
    if re.search(r's[0-9]', line, re.I) is None: 
      m.write(line) 
#   print("moo", line) 
    else: 
      t.write(line) 
#   print("tmp", line) 
t.close 
m.close 
os.rename(tmp, ttv) 

# Remove badshows 
t = open(tmp, 'w') 
with open(bad) as f: 
    content = f.readlines() 
bap = [x.strip() for x in content] 

with open(ttv) as f: 
    content = f.readlines() 
all = [x.strip() for x in content] 

for line in all: 
    flag = True 
    for b in bap: 
     if b in line: 
      flag = False 
      break 
    if flag: 
     t.write(line + "\n") 
t.close 
os.rename(tmp, ttv) 
+0

Pythonはあまりにも悪くありません。また、 '#Function not_present'を改善することもできます。現在、毎回ファイルを読み込んでいます。ファイルを一度読み込んでリストに保存します。メソッドが呼び出されると、そのリストからチェックします。 –

関連する問題