Output data table with pipe delimiter

I got stuck with having to create a CSV file in UTF-8 format without BOM. Luckily this link made that easy

The problem I am having now is that I need the output to be pipe delimited and the “Output Data Table” function seems to default to csv without any options. Any ideas?

1 Like

Hey @FMCgclifford

Kindly try replacing the comma with pipe symbol in the output data table string.

Thanks
#nK

that won’t work as some of my value columns might also contain commas. They would be quoted in the csv

a,b,c,d,e
value1,value2,value3,value4,value5
“value 1,a”,value2,value3,value4,value5

You can use Regex.Replace to get around the issue:

newCSV = System.Text.RegularExpressions.Regex.Replace(outputFromDT, ",(?=(?:[^""]*""[^""]*"")*[^""]*$)", "|")

ptrobot,

This will solve the problem of commas inside quoted strings?

Graham

Yes, it will only replace commas outside quotes.

image

I’m still testing this. My data table is large (the CSV is about 15MB). I’ve been waiting 2 hours for this additional command to finish and it is still running. Any idea why it would take so long? I cannot wait hours for this conversion unfortunately.

Thoughts?

Unfortunately it will take awhile to replace a big string, especially with regex.

If you don’t mind real code, here’s an alternative way with Invoke Code:

Just change the Delimiter to “|” before using the code.

Interesting that I added one block of the invoke code and it seems ok.

I added a second one further down in the workflow and that gives an error. Any ideas why?

image

Have you imported CSVHelper?

we would recommend not to create artifacts which are not in a format as needed and the adoapt by regex/replace.

We can generate the CSV as needed directly:

Find starter Help here:
WriteMassCSV_UTFWithoutBOM_CustomDelimiter.xaml (8.4 KB)

i added that to the import. New error now.

No compiled code to run
error BC30512: Option Strict On disallows implicit conversions from ‘System.IO.StreamWriter’ to ‘CsvHelper.ISerializer’. At line 2
error BC30456: ‘Encoding’ is not a member of ‘CsvHelper.Configuration.IWriterConfiguration’. At line 5

my arguments are a DT (with the data) and a string for the filename

Test with the code below instead. I have hard coded the namespace so UiPath shouldn’t complain anymore.

Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(in_FileName)
Dim csv As CsvHelper.CsvWriter = New CsvHelper.CsvWriter(sw)

csv.Configuration.Delimiter = "|"
csv.Configuration.Encoding = New System.Text.UTF8Encoding(False)

' Write headers
For Each col As System.Data.DataColumn In in_DT.Columns
	csv.WriteField(col.ColumnName)
Next col
csv.NextRecord

' Write rows
For Each row As System.Data.DataRow In in_DT.Rows
	For i As Integer = 0 To row.ItemArray.Count-1
		csv.WriteField(row(i))
	Next
	csv.NextRecord
Next row

sw.Close

' Clean up
sw.Dispose
csv.Dispose

Also remove the CsvHelper import you added. It seems like it has imported an older version of CsvHelper that are missing a lot of features.

Unfortunately i’m seeing the same error still. I am using your new code and I removed the csvhelpder from the import tab.

Error ERROR Validation Error No compiled code to run
error BC30512: Option Strict On disallows implicit conversions from ‘System.IO.StreamWriter’ to ‘CsvHelper.ISerializer’. At line 2
error BC30456: ‘Encoding’ is not a member of ‘CsvHelper.Configuration.IWriterConfiguration’. At line 5 Main.xaml

Between the first Invoke Code and the second Invoke Code, do you have any other CSV activities? It seems like your UiPath Studio is trying to access different versions of CsvHelper.

If you search of CsvHelper in your project, do you see different versions?
image

I have no issue invoking the code several times on my computer:

Here is mine

image

Here’s my last try. The code has been updated for CsvHelper 16.0.0. I have also removed the encoding line since CsvHelper doesn’t add BOM either way.

Dim sw As System.IO.StreamWriter = System.IO.File.CreateText(in_FileName)
Dim csv As CsvHelper.CsvWriter = New CsvHelper.CsvWriter(sw, System.Globalization.CultureInfo.InvariantCulture)

csv.Configuration.Delimiter = "|"

' Write headers
For Each col As System.Data.DataColumn In in_DT.Columns
	csv.WriteField(col.ColumnName)
Next col
csv.NextRecord

' Write rows
For Each row As System.Data.DataRow In in_DT.Rows
	For i As Integer = 0 To row.ItemArray.Count-1
		csv.WriteField(row(i))
	Next
	csv.NextRecord
Next row

sw.Close

' Clean up
sw.Dispose
csv.Dispose

Thank you!!! that did it.

Thank you again.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.