Python Forum
RegExp: returning 2nd loop in new document
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RegExp: returning 2nd loop in new document
#1
Hello, I am new to this forum. I joined so that I could ask for help with this program. First, I will detail what it does.

I get these search files that look a bit like this:
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2001.epub ::INFO:: 344.3KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2001.zip ::INFO:: 373.7KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2002.epub ::INFO:: 493.9KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2002.mobi ::INFO:: 546.6KB
!sfshine Robert Silverberg & Karen Haber (ed) - Science Fiction-The Best of 2002.zip ::INFO:: 297.2KB

I only have a use for certain types of files. Now, I can get my script to filter one or several at a time by using a for in loop. I thought that if I added a second loop, I could get them sorted into different piles. So, here is the code:
*****************************
import re

# input the name of the file
input_file_name = input("Enter file name: ")
input_file_name = input_file_name + ".txt"
input_open = open(input_file_name)

# create new file name
file_type_removed = input_file_name[:-4] #  removes .txt from input file name
add_modified_to_new_file = "_modified.txt" #  creates the end part of the new file name
new_text = file_type_removed + add_modified_to_new_file #  puts the file extension and modified to the new file

new_file_create = open(new_text, "w+") # should create a new file with this name

#  finds all the lines with mobi in them and writes those lines to the new file
for line in input_open.readlines():
    if re.search('mobi|azw', line):
        new_file_create.write(line)

#  This second loop will not run or return the results as I expected. Only the first loop 
#  returns a text file with the expected results. I would like to understand why and how 
#  I might fix it.
for line in input_open.readlines():
    if re.search('pdf', line):
        new_file_create.write(line)


# Close the files
new_file_create.close()
input_open.close()
***********************************
I also have a second question that I have been thinking about: I have thought about building a library of authors that
sometimes I want to search for. So, I wondered if using a similar formula to what I have already but as a function where
the loop iterates through the dictionary looking for and returning lines which contain the name of the given author. Is
that a valid way of going about it, or should I be seeking a different path?

Any help is appreciated,
Steven
Reply


Messages In This Thread
RegExp: returning 2nd loop in new document - by syoung - May-02-2018, 04:06 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Returning data on button click by buttons created by a loop bradells 3 596 Apr-23-2025, 03:01 PM
Last Post: Pedroski55
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 2,711 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  For Loop Returning 3 Results When There Should Be 1 knight2000 12 6,375 Sep-27-2021, 03:18 AM
Last Post: SamHobbs
  returning values in for loop Nickd12 4 23,629 Dec-17-2020, 03:51 AM
Last Post: snippsat
  Check for funny characters with a regexp bertilow 4 4,051 Jan-19-2020, 10:16 AM
Last Post: bertilow
  Returning true or false in a for loop bbop1232012 3 11,477 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  Regexp that won't match anything Ofnuts 4 5,175 Mar-17-2017, 02:48 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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