Created
November 18, 2024 07:30
-
-
Save konojunya/0a6a1637e78dcea052e060878e4fb281 to your computer and use it in GitHub Desktop.
csv script
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 ( | |
"encoding/csv" | |
"fmt" | |
"os" | |
"path/filepath" | |
) | |
func main() { | |
input := "./input" | |
output := "./output.csv" | |
// アウトプットのCSVファイルを作成 | |
outFile, err := os.Create(output) | |
if err != nil { | |
fmt.Println("Error creating output file:", err) | |
return | |
} | |
defer outFile.Close() | |
// CSVファイルに書き込むためのライターを作成 | |
writer := csv.NewWriter(outFile) | |
defer writer.Flush() | |
// 入力フォルダ内のCSVファイルを取得 | |
files, err := filepath.Glob(filepath.Join(input, "*.csv")) | |
if err != nil { | |
fmt.Println("Error reading input files:", err) | |
return | |
} | |
for _, file := range files { | |
fmt.Println("Processing file:", file) | |
// CSVファイルを開く | |
inFile, err := os.Open(file) | |
if err != nil { | |
fmt.Println("Error opening input file:", err) | |
// 次のファイルを処理 | |
continue | |
} | |
defer inFile.Close() | |
reader := csv.NewReader(inFile) | |
// CSVファイルの内容を読み込む | |
var rowCount int | |
for { | |
row, err := reader.Read() | |
if err != nil { | |
break | |
} | |
rowCount++ | |
// 15行目からスタートして、1000行おきに選択 | |
if (rowCount-15)%1000 == 0 && rowCount >= 15 { | |
if err := writer.Write(row); err != nil { | |
fmt.Println("Error writing row:", err) | |
} | |
} | |
} | |
} | |
fmt.Println("Processing complete. Output file:", output) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment