Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/csv"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. )
  10.  
  11. type Tweets []struct {
  12. Fullname string `json:"fullname"`
  13. ID string `json:"id"`
  14. Text string `json:"text"`
  15. Timestamp string `json:"timestamp"`
  16. User string `json:"user"`
  17. }
  18.  
  19. func main() {
  20. if len(os.Args) < 3 {
  21. fmt.Printf("Usage: %s infile.json outfile.csv (order is important, name doesn't matter)", os.Args[0])
  22. os.Exit(1)
  23. }
  24.  
  25. infile, err := ioutil.ReadFile(os.Args[1])
  26. if err != nil {
  27. panic(err)
  28. }
  29.  
  30. var JSONTweets Tweets
  31. err = json.Unmarshal([]byte(infile), &JSONTweets)
  32. if err != nil {
  33. panic(err)
  34. }
  35.  
  36. out, err := os.Create(os.Args[2])
  37. if err != nil {
  38. panic(err)
  39. }
  40. defer out.Close()
  41.  
  42. w := csv.NewWriter(out)
  43.  
  44. w.Write([]string{"timestamp", "text"})
  45. for _, obj := range JSONTweets {
  46. var record []string
  47. record = append(record, obj.Timestamp)
  48. record = append(record, obj.Text)
  49. w.Write(record)
  50. }
  51. w.Flush()
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement