How python-csv converts text to csv File
Python-csv how to convert text to csv file, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this can learn, I hope you can gain something.
import csv def txt2csv(inputfile,outputfile): datacsv = open(outputfile,'w') csvwriter = csv.writer(datacsv,dialect=("excel")) mainfileH = open(inputfile,'rb') for line in mainfileH.readlines(): print "Debug: " + line.replace('\n','') csvwriter.writerow([a for a in line.replace('\n','').split('#')]) datacsv.close() mainfileH.close() Note: Before calling txt2csv, confirm that txtfile is closed (), which has been encountered before. If there is no txtfile.close(), the txtfile file read by readlines is only 8192 bytes, and the characters after it are not read. Also, the newline character in txtfile needs to be replaced by '' by replace, otherwise it will be converted to csv, and there will be a blank line between every two lines. txt2csv (txtfile,csvFile) Is it helpful to read the above? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.