|
@@ -0,0 +1,34 @@
|
|
|
+import Papa from 'papaparse'
|
|
|
+export function parseCSV(url) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ Papa.parse(url, {
|
|
|
+ download: true,
|
|
|
+ header: true,
|
|
|
+ // dynamicTyping: true,
|
|
|
+ transform: (value, header) => {
|
|
|
+ // 检查是否是科学记数法
|
|
|
+ const scientificNotationRegex =
|
|
|
+ /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/
|
|
|
+
|
|
|
+ // 如果是科学记数法,将其转换为普通数字格式
|
|
|
+ if (scientificNotationRegex.test(value)) {
|
|
|
+ // const numberValue = Number(value);
|
|
|
+ // return isNaN(numberValue) ? value : numberValue;
|
|
|
+ return parseFloat(value)
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果不是科学记数法,保持原值
|
|
|
+ return value
|
|
|
+ },
|
|
|
+ complete: results => {
|
|
|
+ // 处理完整的解析结果
|
|
|
+ console.log(results.data)
|
|
|
+ resolve(results.data)
|
|
|
+ },
|
|
|
+ error: (error) => {
|
|
|
+ console.error('Error parsing CSV:', error.message);
|
|
|
+ reject(error.message);
|
|
|
+ },
|
|
|
+ })
|
|
|
+ })
|
|
|
+}
|