python - Getting data from csv -
i getting data frommy csv fields "('value',)". how can remove value. searching yesterday nothing worked me. please me. in advance.
my model
from django.db import models # create 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 django.shortcuts import render .forms import uploadfileform django.shortcuts import httpresponseredirect .models import csvreader, uploadfile def csv(request): if request.method == "post": form = uploadfileform(request.post, request.files) if form.is_valid(): = uploadfile.objects.create() up.upload = request.files['upload'] up.save() path='c:\data_zxcwwnf.csv' remove = "'()," open(path, 'rb') csvfile: reader = csv.reader(csvfile) 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',)
you should replace 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]
Comments
Post a Comment