2012-08-15 21 views
9

मैं एक पाठ फ़ाइल से एक पाठ पढ़ने के लिए कोशिश कर रहा हूँ शामिल निकालें, लाइनों को पढ़ने, लाइनों है कि विशिष्ट स्ट्रिंग होते हैं (इस मामले 'बुरे' और 'शरारती' में) को हटा दें। कोड मैंने लिखा इस प्रकार है:लाइनों है कि कुछ स्ट्रिंग

infile = file('./oldfile.txt') 

newopen = open('./newfile.txt', 'w') 
for line in infile : 

    if 'bad' in line: 
     line = line.replace('.' , '') 
    if 'naughty' in line: 
     line = line.replace('.', '') 
    else: 
     newopen.write(line) 

newopen.close() 

मैं इस तरह लिखा था लेकिन यह काम नहीं करता।

एक बात महत्वपूर्ण है, है अगर पाठ की सामग्री के इस तरह था:

good baby 
bad boy 
good boy 
normal boy 

मैं उत्पादन खाली लाइनों के लिए नहीं करना चाहती। इसलिए पसंद नहीं:

good baby 

good boy 
normal boy 

लेकिन इस तरह:

good baby 
good boy 
normal boy 

क्या मैं ऊपर पर मेरे कोड से संपादित करना चाहिए?

+0

तुम क्यों लाइनों तुम वैसे भी अनदेखा करना चाहते में रिक्तियों के साथ डॉट्स की जगह कर रहे हैं? – geoffspear

+0

@Wooble हो सकता है कि ओपी कि एक नियमित अभिव्यक्ति है, जहां वह '' nothing' साथ 'line' में' anything' की सभी घटनाओं replace' हैं हो जाता है। – jadkik94

उत्तर

32

आप अपने कोड को सरल और इस

bad_words = ['bad', 'naughty'] 

with open('oldfile.txt') as oldfile, open('newfile.txt', 'w') as newfile: 
    for line in oldfile: 
     if not any(bad_word in line for bad_word in bad_words): 
      newfile.write(line) 

की तरह अधिक पठनीय एक Context Manager और any का उपयोग कर बना सकते हैं।

4

आप बस के बजाय की जगह करने के नए फ़ाइल में लाइन में शामिल नहीं कर सका।

for line in infile : 
    if 'bad' not in line and 'naughty' not in line: 
      newopen.write(line) 
+1

मैं आप "या" के बजाय चाहते हैं लगता है "और" –

+4

मुझे लगता है कि "और" सही –

+0

मैं चाहता लाइनों है कि केवल बुरा या naghuty में से एक में शामिल भी हटाए जाने के लिए है। कौनसा सही हैं..? –

1

else केवल पिछले if से जुड़ा है। आप elif हैं:

if 'bad' in line: 
    pass 
elif 'naughty' in line: 
    pass 
else: 
    newopen.write(line) 

भी ध्यान रखें कि मैं लाइन प्रतिस्थापन हटा दिया, जैसा कि आप उन पंक्तियों वैसे भी नहीं लिखते।

0
to_skip = ("bad", "naughty") 
out_handle = open("testout", "w") 

with open("testin", "r") as handle: 
    for line in handle: 
     if set(line.split(" ")).intersection(to_skip): 
      continue 
     out_handle.write(line) 
out_handle.close() 
+0

वहाँ की तरह 'यह बुरा है कुछ करता है, तो काम नहीं करेगा!' इनपुट फ़ाइल में। – sloth

0

आज मुझे एक समान कार्य पूरा करने की आवश्यकता है इसलिए मैंने कुछ शोधों के आधार पर कार्य को पूरा करने के लिए एक गलती लिखी। मुझे आशा है कि किसी को यह उपयोगी मिल जाएगा!

import os 

os.system('cls' if os.name == 'nt' else 'clear') 

oldfile = raw_input('{*} Enter the file (with extension) you would like to strip domains from: ') 
newfile = raw_input('{*} Enter the name of the file (with extension) you would like me to save: ') 

emailDomains = ['windstream.net', 'mail.com', 'google.com', 'web.de', 'email', 'yandex.ru', 'ymail', 'mail.eu', 'mail.bg', 'comcast.net', 'yahoo', 'Yahoo', 'gmail', 'Gmail', 'GMAIL', 'hotmail', 'comcast', 'bellsouth.net', 'verizon.net', 'att.net', 'roadrunner.com', 'charter.net', 'mail.ru', '@live', 'icloud', '@aol', 'facebook', 'outlook', 'myspace', 'rocketmail'] 

print "\n[*] This script will remove records that contain the following strings: \n\n", emailDomains 

raw_input("\n[!] Press any key to start...\n") 

linecounter = 0 

with open(oldfile) as oFile, open(newfile, 'w') as nFile: 
    for line in oFile: 
     if not any(domain in line for domain in emailDomains): 
      nFile.write(line) 
      linecounter = linecounter + 1 
      print '[*] - {%s} Writing verified record to %s ---{ %s' % (linecounter, newfile, line) 

print '[*] === COMPLETE === [*]' 
print '[*] %s was saved' % newfile 
print '[*] There are %s records in your saved file.' % linecounter 

सार के लिए लिंक: emailStripper.py

बेस्ट, Az

0

उपयोग अजगर-textops पैकेज:

from textops import * 

'oldfile.txt' | cat() | grepv('bad') | tofile('newfile.txt') 
0

मैं इस का इस्तेमाल किया है पाठ फ़ाइलों से अवांछित शब्दों को हटाने के लिए:

bad_words = ['abc', 'def', 'ghi', 'jkl'] 

with open('List of words.txt') as badfile, open('Clean list of words.txt', 'w') as cleanfile: 
    for line in badfile: 
     clean = True 
     for word in bad_words: 
      if word in line: 
       clean = False 
     if clean == True: 
      cleanfile.write(line) 

या एक निर्देशिका में सभी फ़ाइलों के लिए भी ऐसा ही करने के लिए:

import os 

bad_words = ['abc', 'def', 'ghi', 'jkl'] 

for root, dirs, files in os.walk(".", topdown = True): 
    for file in files: 
     if '.txt' in file: 
      with open(file) as filename, open('clean '+file, 'w') as cleanfile: 
       for line in filename: 
        clean = True 
        for word in bad_words: 
         if word in line: 
          clean = False 
        if clean == True: 
         cleanfile.write(line) 

मुझे यकीन है कि यह करने के लिए एक और अधिक सुरुचिपूर्ण तरीका होना चाहिए हूँ, लेकिन यह है कि क्या मैं इसे करना चाहता था था।

संबंधित मुद्दे