JMeter 将同一列中带逗号的值写入 CSV

JMeter write to CSV with values with comma in the same column

我正在将一些值写入 CSV。其中一列值有一个逗号,如“Hello, World”。 当我写入 CSV 时,它在第 1 列中写入 Hello,在第 2 列中写入 World。

我希望两者都写在同一栏中。我尝试了很多选择但没有运气。这是我使用的代码

**///////////////Create CSV File**
import java.io.*;
import java.lang.*;
import java.util.*;
import java.io.FileOutputStream;

String path = "ExtractedFiles/";
String dataExtractionCSVFile = path+ "ExtractedData.${DateTime}.csv";
f = new FileOutputStream(dataExtractionCSVFile, true);
p = new PrintStream(f); 
this.interpreter.setOut(p);
print("comments");
f.close();
vars.put("dataExtractionCSVFile",dataExtractionCSVFile)

/////////////将值写入 CSV 文件

import java.io.FileWriter;
comments = vars.get("comments");

dataExtractionCSVFile = vars.get("dataExtractionCSVFile");
f = new FileOutputStream(dataExtractionCSVFile, true);
        p = new PrintStream(f);  
        this.interpreter.setOut(p);     
        print(comments);
        f.close();

CSV 中没有“列”这样的概念,如果您使用逗号作为分隔符并且您的 comments 包含逗号,则解决方案将用引号将其括起来。

类似于:

print("\"" + comments + "\"");

如果您要在 CSV Data Set Config 中重新使用 CSV 文件 - 请务必将“允许引用数据”设置为 True

另请注意,使用脚本将数据写入文件存在潜在危险,如果 运行 将脚本与超过 1 个线程(用户)结合使用,您可能 运行 race condition so either make sure to put your script under the Critical Section Controller or switch to i.e. Flexible File Writer