-
Notifications
You must be signed in to change notification settings - Fork 0
/
converter.go
59 lines (52 loc) · 1.75 KB
/
converter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"bufio"
"fmt"
"log"
"os"
"path/filepath"
"strings"
)
func visit(path string, f os.FileInfo, err error) error {
if strings.Contains(path, "dat") {
fmt.Printf(path + "\n")
getConvert(path, f)
}
//fmt.Printf("Visited: %s\n", path)
return nil
}
func getConvert(path string, f os.FileInfo) {
file, _ := os.Open(path)
scanner := bufio.NewScanner(file)
var firstLine bool = true
var firstColumn bool = true
var lineContext string = ""
fileb, _ := os.Create(strings.Replace(f.Name(), ".dat", "", -1) + ".csv")
writer := bufio.NewWriter(fileb)
for scanner.Scan() {
if firstLine == false {
lineContext = strings.Replace(strings.Replace(scanner.Text(), " ", "", -1), "2013", "2013 ", -1)
for _,i := range strings.Split(lineContext,";"){
if firstColumn {
writer.WriteString(i)
firstColumn = false
}else{
writer.WriteString(";"+strings.Replace(i,".",",",-1))
}
}
writer.WriteString("\n")
firstColumn = true
} else {
firstLine = false
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
writer.Flush()
file.Close()
}
func main() {
err := filepath.Walk(".", visit)
fmt.Printf("filepath.Walk() returned %v\n", err)
}