Skip to content

Instantly share code, notes, and snippets.

@jmazzi
Last active December 20, 2015 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmazzi/6118632 to your computer and use it in GitHub Desktop.
Save jmazzi/6118632 to your computer and use it in GitHub Desktop.
Convert a LastPass CSV export to keepassx using Go lang.
package main
import (
"encoding/csv"
"fmt"
"io"
"os"
"strings"
"html"
)
func main() {
file, error := os.Open("lastpass.csv")
if error != nil {
fmt.Println("Error:", error)
return
}
defer file.Close()
reader := csv.NewReader(file)
lineCount := 0
fmt.Printf("<!DOCTYPE KEEPASSX_DATABASE>\n")
fmt.Printf("<database>\n")
fmt.Printf(" <group>\n")
fmt.Printf(" <title>Main</title>\n")
for {
record, error := reader.Read()
if error == io.EOF {
break
} else if error != nil {
fmt.Println("Error:", error)
return
}
// url,username,password,extra,name,grouping,fav
url := html.EscapeString(strings.TrimSpace(record[0]))
username := html.EscapeString(strings.TrimSpace(record[1]))
password := html.EscapeString(record[2])
name := html.EscapeString(record[4])
// group := html.EscapeString(record[5])
fmt.Printf(" <entry>\n")
fmt.Printf(" <title>%s</title>\n", name)
fmt.Printf(" <username>%s</username>\n", username)
fmt.Printf(" <password>%s</password>\n", password)
fmt.Printf(" <url>%s</url>\n", url)
fmt.Printf(" <comment></comment>\n")
fmt.Printf(" </entry>\n")
lineCount += 1
}
fmt.Printf(" </group>\n")
fmt.Printf("</database>\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment