第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Django:從 CSV 導入數(shù)據(jù) - 元組索引必須是整數(shù)或切片,而不是 str

Django:從 CSV 導入數(shù)據(jù) - 元組索引必須是整數(shù)或切片,而不是 str

明月笑刀無情 2022-01-05 10:45:29
在我的 Django APP 中,我想將數(shù)據(jù)從 CSV 上傳到模型。為了讀取數(shù)據(jù),我正在使用pandas庫。但我收到此錯誤:文件“D:\web_proyects\stickers-gallito-app\shop\management\commands\categories.py”,第 23 行,在 tmp_data_categories.iterrows() 中的行類型錯誤:元組索引必須是整數(shù)或切片,而不是 str我在想是不是因為我如何制定我的 for 循環(huán)來讀取數(shù)據(jù)。模型.py:class Category(models.Model):    category = models.CharField(max_length=250, unique=True)    slug = models.SlugField(max_length=250, unique=True)    description = models.TextField(blank=True)    image = models.ImageField(upload_to='category', blank=True, null=True)    video = EmbedVideoField(null=True, blank=True)    class Meta:        ordering = ('category',)        verbose_name = 'category'        verbose_name_plural = 'categories'    def get_url(self):        return reverse('shop:allCat', args=[self.slug])    def __str__(self):        return '{}'.format(self.name)命令/類別.py:import pandas as pdimport csvfrom shop.models import Categoryfrom django.core.management.base import BaseCommandtmp_data_categories=pd.read_csv('static/data/categories.csv',sep=',', encoding="utf-8")class Command(BaseCommand):    def handle(self, **options):        categories = [            Category(                category=row['category'],                slug=row['product'],                subcategory=row['slug'],                subcategory_slug=row['description'],                description=row['size'],                image =row['quantity'],                video=row['image'],        )            for row in tmp_data_categories.iterrows()        ]        Category.objects.bulk_create(categories)調(diào)用時出現(xiàn)錯誤:python manage.py categories
查看完整描述

2 回答

?
POPMUISE

TA貢獻1765條經(jīng)驗 獲得超5個贊

因為 iter overiteritems返回一個你不能使用字符串索引的 tupe。該元組的第二個元素是可使用字符串名稱下標的 pandas 系列。所以你應該做


categories = [

            Category(

                category=row['category'],

                slug=row['product'],

                subcategory=row['slug'],

                subcategory_slug=row['description'],

                description=row['size'],

                image =row['quantity'],

                video=row['image'],

        )

            for _, row in tmp_data_categories.iterrows()

        ] 


查看完整回答
反對 回復 2022-01-05
?
胡說叔叔

TA貢獻1804條經(jīng)驗 獲得超8個贊

這不起作用,因為 django 的models.py不是類似 dict 的對象。


但是,當您不需要時,為什么要在這里使用熊貓。證人:


tmp_data_categories=csv.DictReader('static/data/categories.csv', fieldnames=['category', 'product', 'slug', 'description', 'size', 'quantity', 'image'])


categories = [

            Category(

                category=row['category'],

                slug=row['product'],

                subcategory=row['slug'],

                subcategory_slug=row['description'],

                description=row['size'],

                image =row['quantity'],

                video=row['image'],

        )

            for row in tmp_data_categories

        ]

希望有幫助。


查看完整回答
反對 回復 2022-01-05
  • 2 回答
  • 0 關注
  • 192 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號