Home > database >  How to avoid double spaces with csv writer
How to avoid double spaces with csv writer

Time:11-15

I try to automatize Django app files (models.py, views.py, forms.py, template) production:

with open('views.py', 'a ', newline='', encoding="UTF8") as f1:
    thewriter = csv.writer(f1,delimiter=' ',quotechar='',escapechar=' ',quoting=csv.QUOTE_NONE)

    thewriter.writerow(['from django.shortcuts import render, get_object_or_404, redirect',])
    thewriter.writerow(['@method_decorator(login_required, name="dispatch")',])
    thewriter.writerow(['class PatientCreate(SuccessMessageMixin, CreateView): ',])
...

But my issue is that it 'converts' simple space with double space because of escapechar = ' '. I can not remove this parameters as I need quoting=csv.QUOTE_NONE, otherwise all my lines are delimited by double quotes.

Expected output

from django.shortcuts import render, get_object_or_404, redirect
@method_decorator(login_required, name="dispatch")
class PatientCreate(SuccessMessageMixin, CreateView): 

Current output (note double spaces):

from django.shortcuts  import render,  get_object_or_404,  redirect
@method_decorator(login_required,  name="dispatch")
class  PatientCreate(SuccessMessageMixin,  CreateView): 

CodePudding user response:

Going off what @Barmar pointed out, how about just writing lines:

lines = [
    'from django.shortcuts import render, get_object_or_404, redirect',
    '@method_decorator(login_required, name="dispatch")',
    'class PatientCreate(SuccessMessageMixin, CreateView): ',
]

lines = [line   '\n' for line in lines]  # add your own line endings

# UTF-8 is default
with open('views.py', 'a ', newline='') as f:
    f.writelines(lines)


And because I trawl SO for "CSV" and "Python", here's the CSV-Python solution...

Because your "rows" are single-column, you'll never actually need the "column delimiter" (which is what CSV is all about, breaking up columns and rows of data)...

So set it to something that isn't already a part of the data. I chose a newline because it looks nice. I added an extra column to the first row, print("foo"), to show what happens if you actually have multiple "columns of code"(?!).

BUT, this is certainly wrong, and I imagine some character creeping into the data/code that breaks this... because you don't want to be encoding lines of Python, you just want to write them.

Enjoy:

import csv

rows = [
    ['from django.shortcuts import render, get_object_or_404, redirect', 'print("foo")'],
    ['@method_decorator(login_required, name="dispatch")'],
    ['class PatientCreate(SuccessMessageMixin, CreateView): '],
]

with open('views.py', 'w', newline='') as f:
    writer = csv.writer(f,
                        delimiter='\n',
                        quotechar='',
                        escapechar='',
                        quoting=csv.QUOTE_NONE
                        )

    for row in rows:
        writer.writerow(row)

Gets me:

from django.shortcuts import render, get_object_or_404, redirect
print("foo")
@method_decorator(login_required, name="dispatch")
class PatientCreate(SuccessMessageMixin, CreateView): 
  • Related