Files with a CSV extension are files that are written and saved in a specific order by separating the data with commas for database users. This type of file is needed when you want to transfer data in Excel. I will develop a program with the Go programming language, which can be converted and saved to.
Used CSV Files
## Used CSV Filesplaka,bolge,il,ilce1,AKDENİZ,ADANA,ALADAĞ1,AKDENİZ,ADANA,CEYHAN1,AKDENİZ,ADANA,ÇUKUROVA1,AKDENİZ,ADANA,FEKE1,AKDENİZ,ADANA,İMAMOĞLU..................3,EGE,AFYONKARAHİSAR,BAŞMAKÇI3,EGE,AFYONKARAHİSAR,BAYAT3,EGE,AFYONKARAHİSAR,BOLVADİN3,EGE,AFYONKARAHİSAR,ÇAY3,EGE,AFYONKARAHİSAR,ÇOBANLAR3,EGE,AFYONKARAHİSAR,DAZKIRI3,EGE,AFYONKARAHİSAR,DİNAR3,EGE,AFYONKARAHİSAR,EMİRDAĞ3,EGE,AFYONKARAHİSAR,EVCİLER3,EGE,AFYONKARAHİSAR,HOCALAR3,EGE,AFYONKARAHİSAR,İHSANİYE3,EGE,AFYONKARAHİSAR,İSCEHİSAR....................14,KARADENİZ,BOLU,DÖRTDİVAN14,KARADENİZ,BOLU,GEREDE14,KARADENİZ,BOLU,GÖYNÜK14,KARADENİZ,BOLU,KIBRISCIK14,KARADENİZ,BOLU,MENGEN14,KARADENİZ,BOLU,MUDURNU....................16,MARMARA,BURSA,GÜRSU16,MARMARA,BURSA,HARMANCIK16,MARMARA,BURSA,İNEGÖL16,MARMARA,BURSA,İZNİK16,MARMARA,BURSA,KARACABEY16,MARMARA,BURSA,KELES................31,AKDENİZ,HATAY,DEFNE31,AKDENİZ,HATAY,ARSUZ31,AKDENİZ,HATAY,PAYAS45,EGE,MANİSA,ŞEHZADELER45,EGE,MANİSA,YUNUSEMRE46,AKDENİZ,KAHRAMANMARAŞ,DULKADİROĞLU46,AKDENİZ,KAHRAMANMARAŞ,ONİKİŞUBAT47,GÜNEYDOĞU ANADOLU,MARDİN,ARTUKLU....................63,GÜNEYDOĞU ANADOLU,ŞANLURFA,HALİLİYE63,GÜNEYDOĞU ANADOLU,ŞANLIURFA,KARAKÖPRÜ65,DOĞU ANADOLU,VAN,TUŞBA65,DOĞU ANADOLU,VAN,İPEKYOLU67,KARADENİZ,ZONGULDAK,KİLİMLİ
Used Libraries
import ( "encoding/csv" "encoding/json" "os" "strconv")
Note: encoding/csv and encoding/json, csv to json converting Libraries.
Steps to Program
We are opening our .csv file first.
src, err := os.Open("illerilceler.csv")if err != nil {panic(err)}defer src.Close()
The .json file was created.
dst, err := os.Create("il-ilce.json")if err != nil { panic(err)}defer dst.Close()
- Then, we open the .csv file that we opened, and we save it to the .json file that we created
records := make([]Record, 0, len(rows)) // We are opening up a number of rows. for _, row := range rows { plaka, _ := strconv.ParseInt(row[0], 0, 64) // parsing. bolge := row[1] il := row[2] ilce := row[3] /*read the data in the first row and add it to the records recorder.*/ records = append(records, Record{ Plaka: plaka, Bolge: bolge, Il: il, Ilce: ilce, }) } /*We're moving the recorder to json.*/ err = json.NewEncoder(dst).Encode(records) if err != nil { panic(err) }
All Codes
package mainimport ( "encoding/csv" "encoding/json" "os" "strconv")type Record struct { Plaka int64 Bolge string Il string Ilce string // High, Low, Close}func main() { src, err := os.Open("illerilceler.csv") if err != nil { panic(err) } defer src.Close() dst, err := os.Create("il-ilce.json") if err != nil { panic(err) } defer dst.Close() rows, err := csv.NewReader(src).ReadAll() if err != nil { panic(err) } records := make([]Record, 0, len(rows)) for _, row := range rows { plaka, _ := strconv.ParseInt(row[0], 0, 64) // parseliyoruz. bolge := row[1] il := row[2] ilce := row[3] records = append(records, Record{ Plaka: plaka, Bolge: bolge, Il: il, Ilce: ilce, }) } err = json.NewEncoder(dst).Encode(records) if err != nil { panic(err) }}
Note : The codes of the project can be found atthis address.
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse