CSV writing strings of text that need a unique delimiter

后端 未结 3 1285
无人及你
无人及你 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 07:04

    As I suggested informally in a comment, unique just means you need to use some character that won't be in the data — chr(255) might be a good choice. For example:

    Note: The code shown is for Python 2.x — see comments for a Python 3 version.

    import csv
    
    DELIMITER = chr(255)
    data = ["itemA", "itemB", "itemC",
            "Sentence that might contain commas, colons: or even \"quotes\"."]
    
    with open('data.csv', 'wb') as outfile:
        writer = csv.writer(outfile, delimiter=DELIMITER)
        writer.writerow(data)
    
    with open('data.csv', 'rb') as infile:
        reader = csv.reader(infile, delimiter=DELIMITER)
        for row in reader:
            print row
    

    Output:

    ['itemA', 'itemB', 'itemC', 'Sentence that might contain commas, colons: or even "quotes".']
    

    If you're not using the csv module and instead are writing and/or reading the data manually, then it would go something like this:

    with open('data.csv', 'wb') as outfile:
        outfile.write(DELIMITER.join(data) + '\n')
    
    with open('data.csv', 'rb') as infile:
        row = infile.readline().rstrip().split(DELIMITER)
        print row
    

提交回复
热议问题