gpt4 book ai didi

python - 从 csv 获取数据

转载 作者:太空宇宙 更新时间:2023-11-04 00:44:17 26 4
gpt4 key购买 nike

我正在从我的 csv 字段中获取一些额外的数据,例如“('value',)”。我怎样才能从我的值(value)中删除它。我从昨天开始搜索,但对我没有任何帮助。请帮我。提前致谢。

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',)

最佳答案

您应该将 View 替换为:

                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]

关于python - 从 csv 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40482148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com