Insert space after separator using CSV record
f = open("file1.csv", "r")
g = open("file2.csv", "w")
a = csv.reader(f, delimiter=";", skipinitialspace=True)
b = csv.writer(g, delimiter=";")
for line in a:
b.writerow(line)
In the above code, I am trying to load file1.csv using a module csv
in Python2.7 and then write it in file2.csv using csv.writer
.
My problem comes from existing spaces (one space) after the delimiter in the input file. I need to remove them in order to process the data later, so I used the argument skipinitialspace=True
for the reader. However, I cannot get the author to print a char space after the delimiter and thus break any subsequent diff
occurrence of the two files.
I tried to use the Sniffer
auto-generation class Dialect
, but I think my input files (coming from a large, complex legacy system, with dozens of fields and poor quoting and escaping) turned out to be too complex for that.
In simpler terms, I am looking for answers to the following questions:
- How do I insert a space character after each separator in
writer
? - However, what are the reasons to prohibit the use of multi-character strings as delimiters?
delimiter="; "
would solve my problem.
source to share
You can wrap your objects file
in proxies that add spaces:
>>> class DelimitedFile(file):
... def write(self, value):
... super(DelimitedFile, self).write(value.replace(";", "; "))
...
>>> f = DelimitedFile("foo", "w")
>>> f.write("hello;world")
>>> f.close()
>>> open("foo").read()
'hello; world'
source to share