2

I am getting some extra data frommy csv fields like "('value',)". How can I remove this from my value. I am Searching from yesterday but nothing worked for me. please help me. Thanks in advance.

My model

from django.db import models

# Create your models here.
class CSVReader(models.Model):
    Run = models.CharField(max_length=100, null=True, blank=True)
    Model = models.CharField(max_length=100, null=True, blank=True)
    Name = models.CharField(max_length=100, null=True, blank=True)
    Odometer = models.CharField(max_length=100, null=True, blank=True)
    VIN = models.CharField(max_length=100, null=True, blank=True)
    BidType = models.CharField(max_length=100, null=True, blank=True)
    Ammount = models.CharField(max_length=100, null=True, blank=True)
    BuyerName = models.CharField(max_length=100, null=True, blank=True)


class UploadFile(models.Model):
    upload = models.FileField(upload_to='csv_files')

My view

# -*- coding: utf-8 -*-
import os, re
import csv
import string

from django.shortcuts import render
from .forms import UploadFileForm
from django.shortcuts import HttpResponseRedirect
from .models import CSVReader, UploadFile


def CSV(request):
    if request.method == "POST":
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            up = UploadFile.objects.create()
            up.upload = request.FILES['upload']
            up.save()

            path='C:\data_ZXcwWnf.csv'

            remove = "'(),"
            with open(path, 'rb') as csvfile:
                reader = csv.reader(csvfile)
                for row in reader:


                    c= CSVReader.objects.create()

                    if row[1]!='':
                        c.Run = row[1].replace("('", '').split("')"),
                        temp = row[1].replace("('", '').split("')"),
                        print temp

                    if row[2] != '':
                        c.Model = row[2].replace(remove,"_"),

                    if row[3] != '':
                        c.Name = row[3].replace(remove,"_"),

                    if row[4] != '':
                        c.Odometer = row[4].replace(remove,"_"),

                    if row[5] != '':
                        c.VIN = row[5].replace(remove,"_"),

                    if row[6] != '':
                        c.BidType = row[6].replace(remove,"_"),

                    if row[7] != '':
                        c.Ammount = row[7].replace(remove,"_"),

                    if row[8] != '':
                        c.BuyerName = row[8].replace(remove,"_")

                    print c.Run, c.Model

                    c.save()

                return HttpResponseRedirect('/home/')
        else:
            print form.errors
            print request.FILES
            return HttpResponseRedirect('/CSV_app/index/')
    else:
        form = UploadFileForm(UploadFile)
        return render(request, 'CSV_app/index.html', {'form': form})

My Output is:

    (['Run#'],)
(['Run#'],) ('Model',)
(['   23-182'],)
(['   23-182'],) ('2013',)
None None
None None
(['   23-183'],)
(['   23-183'],) ('2013',)
None None
None None
(['   23-185'],)
(['   23-185'],) ('2013',)
None None
(['   23-186'],)
(['   23-186'],) ('2013',)
None None
(['   23-187'],)
(['   23-187'],) ('2013',)
1
  • Can you add your input CSV file as well? Commented Nov 8, 2016 at 9:05

2 Answers 2

2

You should replace your view with:

                if row[1]!='':
                    c.Run = row[1]
                    temp = row[1]
                    print temp

                if row[2] != '':
                    c.Model = row[2]

                if row[3] != '':
                    c.Name = row[3]

                if row[4] != '':
                    c.Odometer = row[4]

                if row[5] != '':
                    c.VIN = row[5]

                if row[6] != '':
                    c.BidType = row[6]

                if row[7] != '':
                    c.Ammount = row[7]

                if row[8] != '':
                    c.BuyerName = row[8]                    
Sign up to request clarification or add additional context in comments.

Comments

1

This Code:

c.Run = row[1].replace("('", '').split("')"),
temp = row[1].replace("('", '').split("')"),

Replace with:

c.Run = row[1]
temp = row[1] 

Now it works fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.