N
N
NeedSomeBackup2021-03-26 17:23:15
Django
NeedSomeBackup, 2021-03-26 17:23:15

How to make spaces invisible when exporting CSV in Django?

The goal is to upload a csv file to django models.
There is a CSV file with 7 columns. And the model has 6 values.
Full name, apartment number, ownership, share, size and cadastral number
Another 1 column is the ID, which I do not output.

models.py

from django.db import models


# Create your models here.


class Citizen(models.Model):
  name = models.CharField(max_length = 500)
  flat = models.CharField(max_length = 15)
  size = models.CharField(max_length = 30)
  share = models.CharField(max_length = 30)
  doc = models.CharField(max_length = 300)
  kadastr = models.CharField(max_length = 300)



  def __str__(self):
    return self.name


Example:
name: Ivanov Ivan Petrovich
flat: 15
size: 50m2
share: 2/3
doc: Shared ownership No. 11:11:11313013:192-77/111/2018-2 dated 16.23.2014. share in the right 2/3
kadastr : 66:66:6666011:111

When I import CSV into Django, I have a problem that spaces in name and doc are imported as different values.
For example, if Ivanov Ivan Petrovich should get into the full name, then it is unloaded like this:
name: Ivanov
flat: Ivan
size: Petrovich

If you write Ivanov Ivan Petrovich (continuously) in CSV, then everything is ok and gets into name.

views.py
from django.shortcuts import render
from .forms import CsvModelForm
from .models import Csv
import csv
from citizens.models import Citizen 


# Create your views here.

def upload_file_view(request):
  form = CsvModelForm(request.POST or None, request.FILES or None)
  if form.is_valid():
    form.save()
    form = CsvModelForm() 
    obj = Csv.objects.get(activated=False)
    with open(obj.file_name.path, 'r') as f:
      reader = csv.reader(f)

      for i, row in enumerate (reader):
        if i==0:
          pass
        else:
          row = "".join(row)
          row = row.replace(";", " ")
          #row = row.replace(" ", "")
          row = row.split()
          name = row[1]
          flat = row[2]
          size = row[3]
          share = row[4]
          doc = row[5]
          kadastr = row[6]
          Citizen.objects.create(
            name = str(name),
            flat = str(flat),
            size = str(size),
            share = str(share),
            doc = str(doc),
            kadastr = str(kadastr),

            )
          #print(row)
          #print(type(row))

      obj.activated = True
      obj.save()


     
  return render(request, 'csvs/upload.html', {'form':form})


forms.py
from django import forms
from .models import Csv 

class CsvModelForm(forms.ModelForm):
  class Meta:
    model = Csv
    fields = ('file_name',)


Please tell me what I'm doing wrong? :(
Thank you!

PS: I'm sitting reading the theory, I'm learning)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-03-26
@galaxy

It is necessary to set up the csv reader normally, and not to torment spaces (if at all your csv is fundamentally readable).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question