0

im new into python and i wanna do some searching string in txt file. I've been doing crawling some tweet using TwitterSearch API Twitter and then export it into txt files. In my txt files, it has some sentences like :

1|@tephen_ian|@mypreviana ya yg tabrakan maut itu lo... Antara KA 225 sama 220 thn 1987 itu... Yg bintaro 2 itu td pagi|None
2|@adibnurazis|Warga Jakarta korban tabrakan Bintaro ditanggung Pemprov DKI http://t.co/joKjmTKc0S http://t.co/8ebRc8AjzK|None
3|@randhika1991|Turut brduka cita utk Tabrakan KRL vs Truk LPG di Bintaro. Smg Amal Ibadah Korban yg mninggal ditrima oleh Tuhan YME, Aamiin... :( @KAI121|None
4|@ErhamORI|#Bagas Korban KRL vs Truk BBM: Tewas 5 Orang, Luka Berat 9, Luka Ringan 82: Kecelakaan tabrakan KRL dengan tru... http://t.co/a0lmnWQ0TQ|None
5|@appsari|@rararararatri iya. Ada tabrakan KRL jurusan serpong-tanah abang, nabrak truk solar, trs kebakar. Bnyk korban jiwanya..|None

And from that txt files, i wanna search some sentences that contains a few words / query like 'tabrakan', 'di', 'antara', 'tewas', 'luka', 'selamat' and then print sentences which contains that words.

So far, ive doing this :

#!/usr/bin/python

data = open("dataset.txt", "r")
for line in data:
        line_split = line.rstrip("\n").split("|")
        if line_split[2] == 'tabrakan' 'di' 'antara' 'tewas' 'luka' 'selamat':
                print(line_split[0])

and didnt have any result.

Am i doing wrong? Hope anyone could help, cause im done trying using whoosh or any text classifiers :( I'm sorry if im using Indonesian in my crawl dataset.

6
  • possible duplicate of Search for string in txt file Python Commented Dec 9, 2013 at 19:19
  • 1
    Try to print 'tabrakan' 'di' 'antara' 'tewas' 'luka' 'selamat', and see what it is actually equal to. Commented Dec 9, 2013 at 19:20
  • @guisantogui: What has that got to do with the actual problem at hand here? Commented Dec 9, 2013 at 19:29
  • @guisantogui: the other question doesn't teach anything about finding multiple possible matches. Commented Dec 9, 2013 at 19:31
  • @AshwiniChaudhary where and how im supposed to print? sorry if a dumb question, i just get lost Commented Dec 9, 2013 at 19:35

1 Answer 1

1

Search a list of strings for any sub-string from another list

keywords = ['tabrakan', 'di' ,'antara', 'tewas', 'luka', 'selamat']
for line in data:
    if any(k in line for k in keywords):
        print line
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help, finally I came up with my solution :).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.