Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "bufio"
- "encoding/csv"
- "io"
- "log"
- "os"
- "regexp"
- "strings"
- )
- type Person struct {
- Respondent string
- CollectorID string
- StartDate string
- EndDate string
- IPAddress string
- EmailAddress string
- FirstName string
- LastName string
- CompliancePercentageRelatedTasks string
- TypesOfFrameworks string
- TaskCategories string
- NumberOfUsers string
- NumberOfEmployeesPerTask string
- NumberOfControls string
- NumberOfAuditsPerYear string
- NumberOfPublishedPolicies string
- BiggestProblem string
- CurrentComplianceTools string
- CurrentComplianceMonthlyExpense string
- PreviouslyEvaluatedSoftware string
- AuditReaction string
- InterestLevel string
- ExpectToPayPerSeat string
- ContactInformation string
- }
- type CsvAble interface {
- ToSlice() []string
- }
- func (p Person) ToSlice() []string {
- s := []string{
- p.Respondent,
- p.CollectorID,
- p.StartDate,
- p.EndDate,
- p.IPAddress,
- p.EmailAddress,
- p.FirstName,
- p.LastName,
- p.CompliancePercentageRelatedTasks,
- p.TypesOfFrameworks,
- p.TaskCategories,
- p.NumberOfUsers,
- p.NumberOfEmployeesPerTask,
- p.NumberOfControls,
- p.NumberOfAuditsPerYear,
- p.NumberOfPublishedPolicies,
- p.BiggestProblem,
- p.CurrentComplianceTools,
- p.CurrentComplianceMonthlyExpense,
- p.PreviouslyEvaluatedSoftware,
- p.AuditReaction,
- p.InterestLevel,
- p.ExpectToPayPerSeat,
- p.ContactInformation,
- }
- return s
- }
- func main() {
- csvFile, _ := os.Open("data.csv")
- reader := csv.NewReader(bufio.NewReader(csvFile))
- var people []Person
- for {
- line, error := reader.Read()
- if error == io.EOF {
- break
- } else if error != nil {
- log.Fatal(error)
- }
- pattern := regexp.MustCompile(`[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+`)
- email := pattern.FindString(line[45])
- typesOfFrameworks := []string{line[10], line[11], line[12], line[13], line[14], line[15], line[16]}
- taskCategories := []string{line[17], line[18], line[19], line[20], line[22]}
- people = append(people, Person{
- Respondent: line[0],
- CollectorID: line[1],
- StartDate: line[2],
- EndDate: line[3],
- IPAddress: line[4],
- EmailAddress: email,
- FirstName: line[6],
- LastName: line[7],
- CompliancePercentageRelatedTasks: line[9],
- TypesOfFrameworks: strings.Join(typesOfFrameworks, ", "),
- TaskCategories: strings.Join(taskCategories, ", "),
- NumberOfUsers: line[23],
- NumberOfEmployeesPerTask: line[24] + " Other: " + line[25],
- NumberOfControls: line[26] + " Other: " + line[27],
- NumberOfAuditsPerYear: line[28] + " Other: " + line[29],
- NumberOfPublishedPolicies: line[30] + " Other: " + line[31],
- BiggestProblem: line[32],
- CurrentComplianceTools: line[33] + ", " + line[34] + ", " + line[35] + ", " + line[36] + ", " + line[37] + ", " + line[38],
- CurrentComplianceMonthlyExpense: line[39],
- PreviouslyEvaluatedSoftware: line[40] + " Other: " + line[41],
- AuditReaction: line[42],
- InterestLevel: line[43],
- ExpectToPayPerSeat: line[44],
- ContactInformation: line[45],
- })
- }
- w := csv.NewWriter(os.Stdout)
- for _, record := range people {
- if err := w.Write(record.ToSlice()); err != nil {
- log.Fatalln("error writing record to csv:", err)
- }
- }
- // Write any buffered data to the underlying writer (standard output).
- w.Flush()
- if err := w.Error(); err != nil {
- log.Fatal(err)
- }
- }
Add Comment
Please, Sign In to add comment