javascript - Parsing CSV content with Javascript
问题描述
I searched and tried many solutions including papa parser in angular and functions offered in the related Stackoverflow question...no luck. I always have a problem with fields which contain comma.
CSV file structure is like this: (export from Numbers)
name; address
Jack; 762 Texas Rd, Morganville NJ 07751 USA
...but I know that ";" or "," would have been a possible delimiter.
In papa, I tried to set delimiter '', so I expect papa parser to find the delimiter itself.
this.papa.parse(csv_content,{
delimiter: '',
complete: (result) => {
if(result != undefined){
console.log('Parsed: ', result);
}
}
});
...It didn't papa parsed using ',' and not ';'. It couldn't detect ; as the delimiter.
If I convert all semi columns (;) into commas (,) and try to parse, than address field is splitted.
What is your suggestion?
解决方案
明确设置分隔符,而不是将其留给自动检测。
const csv_content = `name; address
Jack; 762 Texas Rd, Morganville NJ 07751 USA`;
Papa.parse(csv_content, {
delimiter: ';',
complete: (result) => {
if (result != undefined) {
console.log('Parsed: ', JSON.stringify(result.data, null, 2));
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.6.2/papaparse.min.js"></script>
推荐阅读
- firebase - 用于设置外键的 Firestore 引用数据类型
- python-3.x - 将方法移动到另一个 Python 文件
- python - Django Celery autoretry_for 和 retry_policy
- mysql - 无法创建 PoolableConnectionFactory 并打开 JDBC 连接
- java - 将双精度值显式转换为浮点数并将其存储在双精度变量中
- sql - 从表格中的逗号分隔值中获取单个值
- sql - spark sql中的广播连接(Spark 1.6.2)
- excel - UDF 中缺少 ParamArray
- ios - 使用 Core Data 删除可滑动的 TableView 单元格
- node.js - Puppeteer querySelector 返回 null