Python Forum
Extract Strings From Text File - Out Put Results to Individual Files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extract Strings From Text File - Out Put Results to Individual Files
#1
Hi friends,

:)

im a python newbie.

I am trying to extract lines from my input file. Output the result to individual files


import os
	
	
Words=['Pear','Cherry']
	
input_file="c:/Users/Dan/Desktop/Python/Phrases.txt"

	
	
with open(input_file,'r') as inFile:
	    
	 text = inFile.read()
	 sentences = text.split(".")
	
	 for s in Words:
	  with open("%s.txt" % s, "w") as f:
	    for sentence in sentences:
	        if (any(map(lambda word: word in sentence, Words))):
	          f.write(sentence + '\n')
	


the first file results were ok. it extracted the sentences with pear in it

but the next file results - it simply out put the original phrase file.

I am not very experienced and have tried multiple things regarding outputting the results

if some one can take a look at this i would be grateful

I hope i worded my first post correctly

thank you for your time
Reply
#2
You loop over the list Words. But in that loop, you have a map statement that uses the whole list, not the individual word for that iteration of the loop. (Note that s is a bad variable name, and since it's not clear what s is, it's not clear that it's what should be in the map statement.) So you are processing all of the words once for each word. If you look at your output file, I bet it has sentences with cherry in it.

So get rid of the map statement, and replace it with a simple if statement for s being in the sentence.

Note that you are looping through the whole file once for each word. It would probably be more efficient to loop through the file once, store sentences in a dictionary based on words they match, and then after that loop go through the stored sentences and write them to the files.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you for those insights, let me try and experiment.

Something wasnt right, but i wasnt sure how to fix it.

i'll try again
Reply
#4
as a side note
instead of any(map(lambda word: word in sentence, Words))
it's better as any(word in sentence for word in Words)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Hi,

I tried to follow the suggestions, so far, Im at here


import os

Words=['Pear','Cherry']
	
with open('Phrases.txt','r') as inFile:
	    
	 text = inFile.read()
	 sentences = text.split(".")
	
	 for s in Words:
	  with open("%s.txt" % s, "w") as f:
	    for sentence in sentences:
	        if (any(word in sentence for word in Words)):  
	          f.write(sentence + '\n')
I do get 2 files - but not the right ouptut
Due to my errors, of not looping file correctly for the words.
Reply
#6
I modified your code for the last section. it works for me. please try from your end.

for s in Words:
with open("%s.txt" % s, "w") as f:
for sentence in sentences:
if( s in sentence):
f.write(sentence + '\n')
Reply
#7
You are still checking every word for every word. You loop through pear and cherry. The first loop is for pear. And in that loop you check for both pear and cherry. The second loop is for cherry, and in that loop you check for both pear and cherry again. You need to change your if statement to be like Sudhakar's, so it only checks the word for the current loop.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
let me add python quotes for easy understanding.

for s in Words:
      with open("%s.txt" % s, "w") as f:
        for sentence in sentences:
            if( s in sentence):
                f.write(sentence + '\n')
Reply
#9
Thank you for all your help.

I think i overthinked it and went off the wrong track earlier

but this does solve the issue

Thanks again to all and Have a great day!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 1,043 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Updating sharepoint excel file odd results cubangt 1 822 Nov-03-2023, 05:13 PM
Last Post: noisefloor
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
Question Need help for a python script to extract information from a list of files lephunghien 6 1,076 Jun-12-2023, 05:40 PM
Last Post: snippsat
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 981 Jan-23-2023, 04:56 AM
Last Post: deanhystad
  azure TTS from text files to mp3s mutantGOD 2 1,695 Jan-17-2023, 03:20 AM
Last Post: mutantGOD
  Trying to send file to printer with no results. chob_thomas 2 3,362 Dec-21-2022, 07:12 AM
Last Post: Pedroski55
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  extract only text strip byte array Pir8Radio 7 2,924 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
  Extract only certain text which are needed Calli 26 5,844 Oct-10-2022, 03:58 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020