Somebody very wise told me to always use a CSV library for creating CSVs. Outputting commas between text values is tempting until you encounter the edge case where that doesn't work... and quoting the text values works until you need to escape quotes... and escaping the quotes works until you need to escape the escape character... it spirals down until you have either written a CSV library or found one provided by the community! Python's CSV module is excellent, as Adam said above. Oh! Also... I know this is preventative not a cure, but I've stopped using regex and other formatting-naive search-and-replace to edit CSVs. Good luck!