Guest User

Untitled

a guest
Jan 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "bufio"
  5. "encoding/csv"
  6. "io"
  7. "log"
  8. "os"
  9. "regexp"
  10. "strings"
  11. )
  12.  
  13. type Person struct {
  14. Respondent string
  15. CollectorID string
  16. StartDate string
  17. EndDate string
  18. IPAddress string
  19. EmailAddress string
  20. FirstName string
  21. LastName string
  22. CompliancePercentageRelatedTasks string
  23. TypesOfFrameworks string
  24. TaskCategories string
  25. NumberOfUsers string
  26. NumberOfEmployeesPerTask string
  27. NumberOfControls string
  28. NumberOfAuditsPerYear string
  29. NumberOfPublishedPolicies string
  30. BiggestProblem string
  31. CurrentComplianceTools string
  32. CurrentComplianceMonthlyExpense string
  33. PreviouslyEvaluatedSoftware string
  34. AuditReaction string
  35. InterestLevel string
  36. ExpectToPayPerSeat string
  37. ContactInformation string
  38. }
  39.  
  40. type CsvAble interface {
  41. ToSlice() []string
  42. }
  43.  
  44. func (p Person) ToSlice() []string {
  45. s := []string{
  46. p.Respondent,
  47. p.CollectorID,
  48. p.StartDate,
  49. p.EndDate,
  50. p.IPAddress,
  51. p.EmailAddress,
  52. p.FirstName,
  53. p.LastName,
  54. p.CompliancePercentageRelatedTasks,
  55. p.TypesOfFrameworks,
  56. p.TaskCategories,
  57. p.NumberOfUsers,
  58. p.NumberOfEmployeesPerTask,
  59. p.NumberOfControls,
  60. p.NumberOfAuditsPerYear,
  61. p.NumberOfPublishedPolicies,
  62. p.BiggestProblem,
  63. p.CurrentComplianceTools,
  64. p.CurrentComplianceMonthlyExpense,
  65. p.PreviouslyEvaluatedSoftware,
  66. p.AuditReaction,
  67. p.InterestLevel,
  68. p.ExpectToPayPerSeat,
  69. p.ContactInformation,
  70. }
  71. return s
  72. }
  73.  
  74. func main() {
  75. csvFile, _ := os.Open("data.csv")
  76. reader := csv.NewReader(bufio.NewReader(csvFile))
  77. var people []Person
  78. for {
  79. line, error := reader.Read()
  80. if error == io.EOF {
  81. break
  82. } else if error != nil {
  83. log.Fatal(error)
  84. }
  85. pattern := regexp.MustCompile(`[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_.]+`)
  86. email := pattern.FindString(line[45])
  87. typesOfFrameworks := []string{line[10], line[11], line[12], line[13], line[14], line[15], line[16]}
  88. taskCategories := []string{line[17], line[18], line[19], line[20], line[22]}
  89. people = append(people, Person{
  90. Respondent: line[0],
  91. CollectorID: line[1],
  92. StartDate: line[2],
  93. EndDate: line[3],
  94. IPAddress: line[4],
  95. EmailAddress: email,
  96. FirstName: line[6],
  97. LastName: line[7],
  98. CompliancePercentageRelatedTasks: line[9],
  99. TypesOfFrameworks: strings.Join(typesOfFrameworks, ", "),
  100. TaskCategories: strings.Join(taskCategories, ", "),
  101. NumberOfUsers: line[23],
  102. NumberOfEmployeesPerTask: line[24] + " Other: " + line[25],
  103. NumberOfControls: line[26] + " Other: " + line[27],
  104. NumberOfAuditsPerYear: line[28] + " Other: " + line[29],
  105. NumberOfPublishedPolicies: line[30] + " Other: " + line[31],
  106. BiggestProblem: line[32],
  107. CurrentComplianceTools: line[33] + ", " + line[34] + ", " + line[35] + ", " + line[36] + ", " + line[37] + ", " + line[38],
  108. CurrentComplianceMonthlyExpense: line[39],
  109. PreviouslyEvaluatedSoftware: line[40] + " Other: " + line[41],
  110. AuditReaction: line[42],
  111. InterestLevel: line[43],
  112. ExpectToPayPerSeat: line[44],
  113. ContactInformation: line[45],
  114. })
  115. }
  116.  
  117. w := csv.NewWriter(os.Stdout)
  118.  
  119. for _, record := range people {
  120. if err := w.Write(record.ToSlice()); err != nil {
  121. log.Fatalln("error writing record to csv:", err)
  122. }
  123. }
  124.  
  125. // Write any buffered data to the underlying writer (standard output).
  126. w.Flush()
  127.  
  128. if err := w.Error(); err != nil {
  129. log.Fatal(err)
  130. }
  131. }
Add Comment
Please, Sign In to add comment