You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Playwright中将CSV分隔符从逗号(,)改为竖线(|)

用csv-parse将竖线(|)设为CSV分隔符的方法

只需在csv-parse的配置参数中添加delimiter: '|',就能把分隔符从默认的逗号改为竖线,这样含逗号的字段就能被正确识别为单个值。

修改后的完整代码如下:

import { test, expect } from '@playwright/test';
import fs from 'fs';
import path from 'path';
import { parse } from 'csv-parse/sync';
import assert from 'assert';

const records = parse(fs.readFileSync(path.join(__dirname, 'CSV.csv')), {
    columns: true,
    skip_empty_lines: true,
    delimiter: '|' // 指定分隔符为竖线
  });
  
for (const record of records){
  test(`Test Case: ${record.test_case}`, async ({ page }) => {
      //console.log(record.test_case, record.username, record.password);
  });
}

补充说明:

  • delimiter参数用于指定CSV的字段分隔符,设置为'|'后,解析器会以竖线作为列的分界,不会把字段内部的逗号误判成分隔符。
  • 原代码末尾缺失test函数和for循环的闭合括号,上述代码已补全,避免语法错误。

内容的提问来源于stack exchange,提问作者Saravanan Baskar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 17:29:56