Skip to content

Instantly share code, notes, and snippets.

@hephyr
Created July 27, 2018 03:30
Show Gist options
  • Save hephyr/c1a785c02c14c80843b4ef3d9466a8b2 to your computer and use it in GitHub Desktop.
Save hephyr/c1a785c02c14c80843b4ef3d9466a8b2 to your computer and use it in GitHub Desktop.
Export contacts in Google Contacts as csv file, then add +86 for the phone numbers
package main
import (
"encoding/csv"
"os"
"strings"
)
func main() {
file, err := os.Open("google.csv")
if err != nil {
panic(err)
}
defer file.Close()
reader := csv.NewReader(file)
reader.FieldsPerRecord = -1
record, err := reader.ReadAll()
if err != nil {
panic(err)
}
// Writer
Wfile, err := os.Create("contacts.csv")
if err != nil {
panic(err)
}
// Wfile.WriteString("\xEF\xBB\xBF")
defer Wfile.Close()
writer := csv.NewWriter(Wfile)
for index, item := range record {
// 36 38
if index != 0 {
item[36] = EditNum(item[36])
item[38] = EditNum(item[38])
item[27] = ""
}
writer.Write(item)
}
writer.Flush()
}
func EditNum(item string) string {
if item == "" {
return ""
}
t := strings.Split(item, ":::")
var tels []string
for _, tel := range t {
if strings.HasPrefix(tel, "+86") || !strings.HasPrefix(tel, "+") || strings.HasPrefix(tel, "86") {
if strings.HasPrefix(tel, "86") {
tel = "+" + tel
}
tel = strings.Replace(tel, " ", "", -1)
tel = strings.Replace(tel, "\u00a0", "", -1)
tel = strings.Replace(tel, "-", "", -1)
if !strings.HasPrefix(tel, "+") {
tel = "+86" + tel
}
tel = tel[:3] + " " + tel[3:6] + " " + tel[6:10] + " " + tel[10:]
}
tels = append(tels, tel)
}
result := strings.Join(tels, " ::: ")
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment