62 lines
1.6 KiB
Python
62 lines
1.6 KiB
Python
|
import csv
|
||
|
import re
|
||
|
import os
|
||
|
|
||
|
def main():
|
||
|
print("Enter the name of the input file:")
|
||
|
input_file_name = input()
|
||
|
|
||
|
# Open input file
|
||
|
try:
|
||
|
file = open(input_file_name, 'r')
|
||
|
except FileNotFoundError:
|
||
|
print(f'Error: File {input_file_name} not found.')
|
||
|
return
|
||
|
|
||
|
# Create output file
|
||
|
try:
|
||
|
output = open("movies.csv", 'w', newline='')
|
||
|
except IOError:
|
||
|
print('Error: Could not write to file "movies.csv".')
|
||
|
file.close()
|
||
|
return
|
||
|
|
||
|
# Initialize CSV writer
|
||
|
writer = csv.writer(output)
|
||
|
|
||
|
# Regex to match movie title, year, and director
|
||
|
pattern = re.compile(r'(.*?)(?:,\s*(The|An|A))?\s+(\d{4})')
|
||
|
|
||
|
for line in file:
|
||
|
# Parse line using regex
|
||
|
matches = pattern.search(line)
|
||
|
if matches is None:
|
||
|
# Skip lines that don't match the expected format
|
||
|
continue
|
||
|
|
||
|
# Extract movie information
|
||
|
movie_title = matches.group(1).strip()
|
||
|
article = matches.group(2)
|
||
|
year = matches.group(3)
|
||
|
|
||
|
# Adjust movie title format
|
||
|
if article:
|
||
|
movie_title = "{} {}".format(article.strip(), movie_title)
|
||
|
|
||
|
# Output fixed movie title and year to terminal
|
||
|
print("Fixed: {} ({})".format(movie_title, year))
|
||
|
|
||
|
# Write movie title and year to CSV file
|
||
|
try:
|
||
|
writer.writerow([movie_title, year])
|
||
|
except Exception as e:
|
||
|
print(f'Error: Could not write to file "movies.csv". Reason: {e}')
|
||
|
file.close()
|
||
|
output.close()
|
||
|
return
|
||
|
|
||
|
file.close()
|
||
|
output.close()
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|