I have a "CSV" file that uses a multi-character delimiter, so the data looks something like
field1_|#|_field2_|#|_field3_|#|_field4
Is there a way to use the Python CSV module to parse this file?
You can use multiple characters as a delimiter. The Oracle loader documentation says that the delimiter must be a single character, but it doesn't say anything about the field delimiter. So, you can use any number of characters for both delimiters.
Python CSV Module Python provides a CSV module to handle CSV files. To read/write data, you need to loop through rows of the CSV. You need to use the split method to get data from specified columns.
Reading a CSV using Python's inbuilt module called csv using csv. reader object.
csv file format has data items separated by a delimiter other than a comma. This includes semicolon, colon, tab space, vertical bars, etc. In such cases, we need to use the sep parameter inside the read. csv() function.
Try to replace the multichar delimiter with the singlechar one.
Somethin like this:
class DelimitedFile:
def __init__(self, fname, mode='rb', ind=',', outd=','):
self.f = open(fname, mode)
self.ind = ind
self.outd = outd
def __iter__(self):
return self
def next(self):
line = self.f.next()
return line.replace(self.ind, self.outd)
Use it like so:
import csv
delimiter = ','
reader = csv.reader(DelimitedFile(fileName, ind='_|#|_', outd=delimiter))
for row in reader:
print row
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With