1234567891011121314151617181920212223242526272829303132333435363738 |
- const fs = require('fs');
- const path = require('path');
- function writePredictionsToFile(predictions) {
- const root = process.cwd(); // 获取项目根目录
- const assetsPath = path.join(root, 'src/assets/');
- const csvFilePath = path.join(assetsPath, '慧农宝_predict.csv');
- // 确保目录存在
- if (!fs.existsSync(assetsPath)) {
- fs.mkdirSync(assetsPath, { recursive: true });
- }
- // 将预测数据转换为CSV格式
- const csvContent = [
- ['日期', '品种', '数值', '指数'].join(','),
- ...predictions.map(pred => [pred.日期, pred.品种, pred.数值, pred.指数].join(','))
- ].join('\n');
- // 写入CSV文件
- fs.writeFileSync(csvFilePath, csvContent, 'utf-8');
- console.log(`Predictions saved to ${csvFilePath}`);
- }
- // 从标准输入读取预测数据
- let inputData = '';
- process.stdin.on('data', chunk => {
- inputData += chunk.toString();
- });
- process.stdin.on('end', () => {
- try {
- const predictions = JSON.parse(inputData);
- writePredictionsToFile(predictions);
- } catch (e) {
- console.error('Error parsing input data:', e);
- }
- });
|