I am learning python and I have made a function to get text from a .nfo file, improving on this block of code:
# Book Author
_author = ["Author:", "Author.."]
logger_nfo.info('Searching Author book...')
with open(nfofile, "r+", encoding='utf-8') as file1:
fileline1 = file1.readlines()
for x in _author: # <--- Loop through the list to check
for line in fileline1: # <--- Loop through each line
line = line.casefold() # <--- Set line to lowercase
if x.casefold() in line:
logger_nfo.info('Line found with word: %s', x)
nfo_author = line
if nfo_author == '':
logger_nfo.warning('Author not found.')
I've made this function:
nfofile_link = "The Sentence.nfo"
search_for = ["Author:", "Author.."]
def search_nfo(nfofile_link, search_for):
logger_nfo.info('Searching nfo')
with open(nfofile_link, "r+", encoding='utf-8') as file1:
fileline1 = file1.readlines()
for x in search_for: # <--- Loop through the list to check
for line in fileline1: # <--- Loop through each line
line = line.casefold() # <--- Set line to lowercase
if x.casefold() in line:
logger_nfo.info('Line found with word: %s', x)
global nfo_author
nfo_author = line
search_nfo(nfofile_link, search_for)
print(nfo_author)
This works, but I'd like to improve it, how can I do that?
An example of the .nfo file's content:
General Information
===================
Title: Agent in Berlin
Author: Alex Gerlis
Read By: Duncan Galloway
Copyright: 2021
Audiobook Copyright: 2021
Genre: Audiobook
Publisher: Canelo
Series Name: Wolf Pack Spies
Position in Series: 01
Abridged: No
Original Media Information
==========================
Media: Downloaded
Source: Audible
Condition: New
File Information
================
Number of MP3s: 42
Total Duration: 11:33:05
Total MP3 Size: 319.23 MB
Encoded At: 64 kbit/s 22050 Hz Mono
ID3 Tags: Set, v1.1, v2.3