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.
+3


source to share


4 answers


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'

      

+2


source


If you left a space that you want to write (remove / restore it during processing), or return it after processing but before writing, this will take care of that.



+1


source


One solution would be to write an object StringIO

and then replace the semicolons with, '; '

or do this while processing the strings if you are doing any other processing.

0


source


As for the first one, I would probably do something like this:

for k, line in enumerate(a):
    if k == 0:
        b.writerow(line)
    else:
        b.writerow(' ' + line) #assuming line is always a string, if not just use str() on it

      

As for the second, I have no idea.

0


source







All Articles