contains commas in it? If the fields of data in your CSV file contain commas, you can protect them by enclosing those data fields in double-quotes (“). The commas that are part of your data will then be kept separate from the commas which delimit the fields themselves. .
. Creating CSV File That contains Comma With Data For example, let’s say that one of our fields contain commas in the description. If our data looked like the below example: RollNo Name Address 12101 Nivetha Mylapore, Chennai 12102 Lavanya Adyar, Chennai 12103 Ram Gopalapuram, Chennai To retain the commas in “Address” column, you can enclose the fields in quotation marks. For example: RollNo, Name, Address 12101, Nivetha, “Mylapore, Chennai” 12102, Lavanya, “Adyar, Chennai” 12103, Ram, “Gopalapuram, Chennai” As you can see, only the fields that contain commas are enclosed in quotes.
If you open this in MS Excel, It looks like as follows 12th Computer Chapter - - Python and CSV Files Roll No 12101 12102 12103 Nivetha Lavanya Ram Mylapore, Chennai Adyar, Chennai Gopalapuram, Chennai Name Address Fig . . (a) CSV Field data with comma in Excel The same goes for newlines (display data in more than one line example Address column) which may be part of your field data. Any fields containing a newline as part of its data need to be enclosed in double-quotes.
For Example RollNo Name Address 12101 Nivetha Mylapore, Chennai 12102 Lavanya Adyar, Chennai 12103 Ram Gopalapuram, Chennai It should be written in CSV file as RollNo, Name, Address 12101, Nivetha, “Mylapore Chennai” 12102, Lavanya, “Adyar Chennai” 12103, Ram, “Gopalapuram Chennai” 12th Computer Chapter - - The Result will look like this Roll No 12101 12102 12103 Nivetha Lavanya Ram Mylapore, Chennai Adyar, Chennai Gopalapuram, Chennai Name Address Fig . . (b) CSV Field Data with newline in Excel . .
Creating CSV File That contains Double Quotes With Data If your fields contain double-quotes as part of their data, the internal quotation marks need to be doubled so that they can be interpreted correctly. For Example, given the