2

I am generating a pipe | delimited CSV file whereby one of the columns contains commands which are executed by a given user in powershell.

User | Date | Command

However, commands are likely appear as below as the pipe character leads one command to another:

command one | command two | command three

How can I treat each command in my python code so that the pipe characters within commands do not affect the delimiting of the CSV file?

I have considered replacing the pipe character with an alternate character however this would ruin the context of the command.

ackroyd
  • 434
  • 6
  • 6
  • 1
    It depends on the flavour of CSV that you're using — what does the consuming process expect? One common option is to enclose the fields containing the delimiter inside double quotes, doubling up any double quotes that appear inside the field: `"User | Date | Command"` for your example. Another option is to escape the delimiter, typically with a backslash: `User \| Date \| Command`. There may be other alternatives, but those are the most common ones. – Jonathan Leffler May 01 '19 at 18:04
  • Thanks, escaping seems to have resolved the problem: `string.replace('|','\|')` – ackroyd May 02 '19 at 09:52
  • Does this answer your question? [Dealing with commas in a CSV file](https://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file) – TylerH Sep 01 '21 at 14:04

0 Answers0