CSV writing strings of text that need a unique delimiter

后端 未结 3 1284
无人及你
无人及你 2021-01-25 06:14

I wrote an HTML parser in python used to extract data to look like this in a csv file:

    itemA, itemB, itemC, Sentence that might contain commas, or colons: li         


        
3条回答
  •  花落未央
    2021-01-25 06:54

    Yes, delimiters separate values within each line of a CSV file. There are two strategies to delimiting text that has a lot of punctuation marks. First, you can quote the values, e.g.:

    Value 1, Value 2, "This value has a comma, <- right there", Value 4
    

    The second strategy is to use tabs (i.e., '\t').

    Python's built-in CSV module can both read and write CSV files that use quotes. Check out the example code under the csv.reader function. The built-in csv module will handle quotes correctly, e.g. it will escape quotes that are in the value itself.

提交回复
热议问题