Find delimiter in txt to convert to csv using Python
You have to use dialect.delimiter
instead of just dialect
because what is returned is of type class Dialect and you need its attribute Dialect.delimiter
rows=csv.reader(f1, delimiter=dialect.delimiter)
Modified code will be as below
import csv
filename_input = 'filein.txt'
filename_output = 'fileout.csv'
with open(filename_input, 'r') as f1, open(filename_output, 'wb') as f2:
dialect = csv.Sniffer().sniff(f1.read(1024), "\t") #### detect delimiters
f1.seek(0)
print(dialect.delimiter)
rows=csv.reader(f1, delimiter=dialect.delimiter)
writer = csv.writer(f2,delimiter=';')
writer.writerows(rows)
Output:
C:\pyp>python.exe txttocsv.py
,
C:\pyp>
Also note that from doc:
sniff(sample, delimiters=None)
Analyze the given sample and return a Dialect subclass reflecting the parameters found. If the optional delimiters parameter is given, it is interpreted as a string containing possible valid delimiter characters.
Hence if the delimiter that you want to find in your text file is something like #
instead of ,
or ;
then you should mention that in sniff
function as second parameter like this:
dialect = csv.Sniffer().sniff(f1.read(1024), '#')
Update: For reading whole file you will need
dialect = csv.Sniffer().sniff(f1.read())