Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How python-csv converts text to csv File

Shulou Source: shulou.com Published: 2022-06-01 17:54:09 10月16日 Update

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.

Tags: Documents help clarity between content only characters this newline characters articles beginners more knowledge blank lines industry information information channels puzzles needs channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn Docker NVidia Redmi