Python:如果字符串包含某些关键字v.2,则从列表中删除某个字符串

时间:2015-02-18 01:10:05

标签: python string list filter

我正在尝试创建一个代码,以从字符串列表中排除字符串中的某些关键字。今天早上在stackoverflow的帮助下,我可以添加可以排除包含特定关键字的某些字符串的代码。 Python: Remove the certain string from list if string includes certain keyword

但是,当我更改数据集时,它不起作用。

# -*- coding: utf-8 -*-

import nltk, json, os, csv, matplotlib, pylab, re
from matplotlib import *
from nltk import *
from pylab import *
from re import *

'Start with empty list'
tweets=[]
tweetsF=[]


for line in open('apple.json'):
    try:
        tweets.append(json.loads(line))
    except:
        pass

keywordFilter=['pie','juice','cinnamon']

for tweet in tweets:
    for key, value in tweet.items():
        if key=='text':
            tweetsF.append(value)

print(tweetsF[:50])
original_list=tweetsF[:50]

tweetsFBK=[str for str in original_list if not any(word in str for word in keywordFilter)]
print (tweetsFBK)

从今天早上的代码到上面的代码,我只更改了 tweetsF 部分,它返回了数据源中的字符串列表。但是,我认为这不是一件容易的事,因为它是像今早一样的问题的字符串列表。

你知道为什么排除部分不会返回任何值(即返回0)。 ?

[EDITED]

original_list=['RT @haussera: Access to Apple Pay customer data, no, but another way? everybody wins - MarketWatch http://t.co/Fm3LE2iTkY', "Landed in the US, tired w horrible migrane. The only thing helping- Connie's new song on repeat. #SoGood #Nashville https://t.co/AscR4VUkMP", 'I wish jacob would be my cinnamon apple', "I've collected 9,112 gold coins! http://t.co/T62o8NoP09 #iphone, #iphonegames, #gameinsight", 'HAHAHA THEY USED THE SAME ARTICLE AS INDEPENDENT http://t.co/mC7nfnhqSw', '@hot1079atl Let me know what you think of the new single "Mirage "\nhttps://t.co/k8DJ7oxkyg', 'RT @SWNProductions: Hey All so we have a new iTunes listing due to our old one getting messed up please resubscribe via the following https…', 'Shawty go them apple bottoms jeans and the boots with the furrrr with furrrr the whole club is looking at her', 'I highly recommend you use MyMedia - a powerfull download manager for the iPhone/iPad.  http://t.co/TWmYhgKwBH', 'Alusckが失われた時間の異常を解消しました http://t.co/peYgajYvQY http://t.co/sN3jAJnd1I', 'Театр радует туземцев! Теперь мой остров стал еще круче! http://t.co/EApBrIGghO #iphone, #iphonegames, #gameinsight', 'RT @AppIeOfficiel: Our iPhone 7    http://t.co/d2vCOCOTqt', 'Я выполнил задание "Подключаем резервы"! Заходите ко мне в гости! http://t.co/ZReExwwbxh #iphone #iphonegames #gameinsight', "RT @Louis_Tomlinson: @JennSelby Google 'original apple logo' and you will see the one printed on my shirt that you reported on. Trying to l…", "I've collected 4,100 gold coins! http://t.co/JZLQJdRtLG #iphone, #iphonegames, #gameinsight", "I've collected 28,800 gold coins! http://t.co/r3qXNHwUdp #iphone, #iphonegames, #gameinsight", 'RT @AppIeOfficiel: Our iPhone 7    http://t.co/d2vCOCOTqt', '“@EleanorDiamonds: truth hurts doesnt it” i still wonder why u didnt tweet the apple shirt pic funny how u only tweet whats convenient for u', "I'm now an E-List celebrity in Kim Kardashian: Hollywood. You can be famous too by playing on iPhone!  http://t.co/HUZSnzu8pO", "RT @Louis_Tomlinson: @JennSelby Google 'original apple logo' and you will see the one printed on my shirt that you reported on. Trying to l…", '【朗報】ぱるると乃木坂生田ちゃんが相思相愛 https://t.co/5QacaMdASN', '【ONE PIECE ドンジャラ】ワンピースのドンジャラがアプリで登場!登場キャ・・・URL→[https://t.co/QVlDXfOG7S] http://t.co/YlV9pwoVZT', "RT @leedsparadise: people@connecting tis shit with larry wtf the apple wasn't about larry this is about supporting the community get that i…", 'RT @AppIeOfficiel: Our iPhone 7    http://t.co/d2vCOCOTqt', "RT @Real_Liam_Payne: Hey everyone I have a new track out for Cheryl it's called I won't break  https://t.co/2rUQbKZkSn enjoy!! ", 'Apple pulls <b>Fitbit</b> trackers fr... https://t.co/IDhDv6w8lA via @fitbit_fan #fitbit | https://t.co/w8dEhQjEf3', "RT @lunaesio: @wyfesio If you were a tropical fruit, you'd be a fine-apple", 'Apple Removes <b>FitBit</b> Fitness T... https://t.co/gpFeYj8heh via @fitbit_fan #fitbit | https://t.co/w8dEhQjEf3', 'Emily_alicexx gathered the Animal Tracks collection http://t.co/aztmTe7rrN http://t.co/FNMNSzDYkB', "RT @leedsparadise: people@connecting tis shit with larry wtf the apple wasn't about larry this is about supporting the community get that i…", "RT @Louis_Tomlinson: @JennSelby Google 'original apple logo' and you will see the one printed on my shirt that you reported on. Trying to l…", 'fogo, ate o apple quicktime falta ao meu pc, ah bom...', '#Kiahnassong https://t.co/GxYyyzcAwT Raising money for sick children at #birminghamchildrenhospital #BBCCiN', "RT @Real_Liam_Payne: Hey everyone I have a new track out for Cheryl it's called I won't break  https://t.co/2rUQbKZkSn enjoy!! ", "I've collected $17844! Who can collect more? It's a challenge! http://t.co/NV4KzSF9zX #gameinsight #iphonegames #iphone", "RT @Louis_Tomlinson: @JennSelby Google 'original apple logo' and you will see the one printed on my shirt that you reported on. Trying to l…", 'RT @ZaynMalikx69: @Louis_Tomlinson is this also a apple logo?. http://t.co/QHlcZpxhc2', 'Emily_alicexx completed the Zigzag of a snake quest http://t.co/aztmTe7rrN http://t.co/dt4m4ifNDV', '【サカつくシュート】カップ戦<サカつくスターズカップ>をクリア!\nhttps://t.co/X528wy2tcx', "Apple: Call It the iWatch and We'll Kill You http://t.co/cgNp0DusYw", "RT @Louis_Tomlinson: @JennSelby Google 'original apple logo' and you will see the one printed on my shirt that you reported on. Trying to l…", 'Урожай собран - 1 300 еды! Ты тоже проверь свои грядки! http://t.co/kZlFe1lmFM #iphone, #iphonegames, #gameinsight', 'RT @AppIeOfficiel: Our iPhone 7    http://t.co/d2vCOCOTqt', "RT @Louis_Tomlinson: @JennSelby Google 'original apple logo' and you will see the one printed on my shirt that you reported on. Trying to l…", "RT @AppIeOfflciaI: WE'RE GIVING A NEW IPHONE 6\nRULES:\n1. Follow @comedyortruth\n2. Fav this.\n3. 15 winners will be chosen! http://t.co/4Y0y7…", '5,000 SUBSCRIBERS Give away! iphone 6 6+ Nexus 6 Note 4 http://t.co/LiauOBl0gw', 'RT @ZaynMalikx69: @Louis_Tomlinson is this also a apple logo?. http://t.co/QHlcZpxhc2', "There's no limit to perfection, now Administrative building is better then it was! http://t.co/i5X4hGU6Mg #gameinsight #iphonegames #iphone", 'Alusckがクエスト癒やしの水をクリアしました http://t.co/peYgajYvQY http://t.co/o9jF6iyhRn', 'Emily_alicexx completed the Connoisseur achievement and received rewards http://t.co/aztmTe7rrN http://t.co/kR6N6auxYn']

2 个答案:

答案 0 :(得分:1)

编辑:看到你的编辑,这可能无法解决你的问题。您可能仍希望整体解析json文件。

原始讯息:

您的输入文件apple.json如何?它可能是一个json文件,你不应该读取并解析它换行。而是尝试类似的事情:

with open('apple.json') as f:
    jsonlist = json.loads(f.read())

for tweet in jsonlist:
    #do things

你的代码中也有一些不好的做法。使用open('apple.json')你打开一个文件但没有关闭它(在这种情况下这不是太糟糕,因为它会在你的脚本到达时自动关闭,但如果你打开很多文件,这可能会导致问题。显式优于隐式)。

其次,您正在使用try: [...] except:来消除所有错误。错误通常是想告诉你一些事情,所以你想要处理它们或者让它们报告,这样你就可以对它们采取行动。如果您收到错误并且您100%确定可以忽略它们,请执行以下操作:

try:
    stuff() #that might throw IndexError
except IndexError as e:
    pass
    #or deal with the error e.g. log 

代码未经过测试。

答案 1 :(得分:1)

如果要从列表中删除所有字符串,如果它们有另一个列表中的一个或多个字符串?如果是的话:

#list of words/strings to remove
filter=['goto','echo','if']
#input list:
fileIn=['echo hello!','set pass=','if exist private_GONE goto _UNLOCK','if exist private goto _LOCK','if not exist private goto MKprivate']
#output lit:
fileOut=[]

for x in fileIn:
 z=0
 for y in filter:
  if(not y in x):
   z=z+1
 if(z==len(filter)):
  fileOut.append(x)

print('\n'.join(fileOut))

但是如果您只想从列表中删除字符串:

import re
#list of words/strings to remove
filter=['goto','echo','if']
#input list:
fileIn=['echo hello!','set pass=','if exist private_GONE goto _UNLOCK','if exist private goto _LOCK','if not exist private goto MKprivate']
#output lit:
fileOut=[]

for x in fileIn:
 temp=x
 for y in filter:
  temp=re.sub(y,'',temp)
 fileOut.append(temp)

print('\n'.join(fileOut))
希望我帮忙!