-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreener_to_list.ts
48 lines (42 loc) · 1.7 KB
/
screener_to_list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import * as fs from 'fs';
import * as csv from 'csv-parser';
// CSV 파일에서 첫 번째 열 데이터를 추출하는 함수
async function extractFirstColumn(filePath: string): Promise<string[]> {
return new Promise<string[]>((resolve, reject) => {
const symbols: string[] = [];
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (row) => {
const firstColumnValue = Object.values(row)[0]; // 첫 번째 열 데이터
symbols.push(firstColumnValue as string);
})
.on('end', () => {
console.log('CSV 파일 처리 완료.');
resolve(symbols);
})
.on('error', (err) => {
console.error('에러 발생:', err);
reject(err);
});
});
}
// 추출된 데이터를 CSV로 저장하는 함수
function saveToCSV(data: string[], outputFilePath: string): void {
const csvContent = data.map((item) => `${item}`).join('\n'); // 데이터를 줄바꿈으로 구분
fs.writeFile(outputFilePath, csvContent, 'utf8', (err) => {
if (err) {
console.error('CSV 저장 중 에러 발생:', err);
} else {
console.log(`CSV 파일이 ${outputFilePath}에 저장되었습니다.`);
}
});
}
// 실행 로직
(async () => {
const inputCsvPath = 'nasdaq_screener.csv'; // 입력 CSV 파일 경로
const outputCsvPath = 'nasdaq_list.csv'; // 출력 CSV 파일 경로
// CSV에서 첫 번째 열 데이터를 추출
const extractedData = await extractFirstColumn(inputCsvPath);
// 추출된 데이터를 CSV 파일로 저장
saveToCSV(extractedData, outputCsvPath);
})();