So I want to convert a simple tab delimited text file into a csv file. If I convert the txt file into a string using string.split('\n') I get a list with each list item as a string with '\t' between each column. I was thinking I could just replace the '\t' with a comma but it won't treat the string within the list like string and allow me to use string.replace. Here is start of my code that still needs a way to parse the tab "\t".
import csv
import sys
txt_file = r"mytxt.txt"
csv_file = r"mycsv.csv"
in_txt = open(txt_file, "r")
out_csv = csv.writer(open(csv_file, 'wb'))
file_string = in_txt.read()
file_list = file_string.split('\n')
for row in ec_file_list:
out_csv.writerow(row)
Why you should always use 'rb' mode when reading files with the
csv
module:What's in the sample file: any old rubbish, including control characters obtained by extracting blobs or whatever from a database, or injudicious use of the
CHAR
function in Excel formulas, or ...Python follows CP/M, MS-DOS, and Windows when it reads files in text mode:
\r\n
is recognised as the line separator and is served up as\n
, and\x1a
aka Ctrl-Z is recognised as an END-OF-FILE marker.csv with a file opened with 'rb' works as expected:
but text mode doesn't:
csv
supports tab delimited files. Supply thedelimiter
argument toreader
:This is how i Do it