76 lines
1.5 KiB
Go
76 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"encoding/csv"
|
|
"fmt"
|
|
"os"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
func main() {
|
|
fmt.Println("Enter the name of the input file:")
|
|
var inputFileName string
|
|
_, err := fmt.Scanln(&inputFileName)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
// Open input file
|
|
file, err := os.Open(inputFileName)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
// Create output file
|
|
output, err := os.Create("movies.csv")
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer output.Close()
|
|
|
|
// Initialize CSV writer
|
|
writer := csv.NewWriter(output)
|
|
defer writer.Flush()
|
|
|
|
// Regex to match movie title, year, and director
|
|
r := regexp.MustCompile(`(.*?)(?:,\s*(The|An|A))?\s+(\d{4})`)
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
line := scanner.Text()
|
|
|
|
// Parse line using regex
|
|
matches := r.FindStringSubmatch(line)
|
|
if len(matches) != 4 {
|
|
// Skip lines that don't match the expected format
|
|
continue
|
|
}
|
|
|
|
// Extract movie information
|
|
movieTitle := strings.TrimSpace(matches[1])
|
|
article := strings.TrimSpace(matches[2])
|
|
year := matches[3]
|
|
|
|
// Adjust movie title format
|
|
if article != "" {
|
|
movieTitle = fmt.Sprintf("%s %s", article, movieTitle) // Removed the comma here
|
|
}
|
|
|
|
// Output fixed movie title and year to terminal
|
|
fmt.Printf("Fixed: %s (%s)\n", movieTitle, year)
|
|
|
|
// Write movie title and year to CSV file
|
|
err = writer.Write([]string{movieTitle, year})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Check for errors from scanner
|
|
if err := scanner.Err(); err != nil {
|
|
panic(err)
|
|
}
|
|
}
|