Created
June 6, 2019 04:50
-
-
Save ueokande/33ba62e620a5e314a9ee540d36029cb4 to your computer and use it in GitHub Desktop.
Convert tab-splitted dictionaly to json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"encoding/json" | |
"errors" | |
"fmt" | |
"os" | |
"strings" | |
) | |
func run(path string) error { | |
f, err := os.Open(path) | |
if err != nil { | |
return err | |
} | |
defer f.Close() | |
s := bufio.NewScanner(f) | |
dic := make(map[string]string) | |
for s.Scan() { | |
line := s.Text() | |
words := strings.Split(line, "\t") | |
if len(words) != 2 { | |
return errors.New("syntax error: " + line) | |
} | |
dic[words[0]] = words[1] | |
} | |
err = s.Err() | |
if err != nil { | |
return err | |
} | |
return json.NewEncoder(os.Stdout).Encode(dic) | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
fmt.Fprintln(os.Stderr, "Usage:", os.Args[0], "DICT_FILE") | |
os.Exit(1) | |
} | |
err := run(os.Args[1]) | |
if err != nil { | |
fmt.Fprintln(os.Stderr, err) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment