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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python dictreader-如何使CSV列名變?yōu)樾懀?/h1>

我有一個(gè)CSV文件,其列名均為大寫。我正在使用csv.dictreader讀取數(shù)據(jù),但需要使用小寫字母的列名稱。我在這里找到此代碼訪問csv標(biāo)頭空白且不區(qū)分大小寫    import csvclass DictReaderInsensitive(csv.DictReader):    # This class overrides the csv.fieldnames property.    # All fieldnames are without white space and in lower case    @property    def fieldnames(self):        return [field.strip().lower() for field in super(DictReaderInsensitive, self).fieldnames]    def __next__(self):        # get the result from the original __next__, but store it in DictInsensitive        dInsensitive = DictInsensitive()        dOriginal = super(DictReaderInsensitive, self).__next__()        # store all pairs from the old dict in the new, custom one        for key, value in dOriginal.items():            dInsensitive[key] = value        return dInsensitiveclass DictInsensitive(dict):    # This class overrides the __getitem__ method to automatically strip() and lower() the input key    def __getitem__(self, key):        return dict.__getitem__(self, key.strip().lower())我的問題是當(dāng)我使用datafile = open(self.ifs_data_file,'rU')        csvDict = DictReaderInsensitive(datafile)        for row in csvDict:            print row            #self.db.ifs_data.insert(**row)            #self.db.commit()我得到這個(gè)錯(cuò)誤Traceback (most recent call last):  File "D:\Development\python\supplier_review\supplier_review.py", line 239, in update_ifs_data    for row in csvDict:  File "D:\Python27_5\lib\csv.py", line 103, in next    self.fieldnames  File "D:\Development\python\supplier_review\supplier_review.py", line 288, in fieldnames    return [field.strip().lower() for field in super(DictReaderInsensitive, self).fieldnames]TypeError: must be type, not classobj
查看完整描述

3 回答

?
一只斗牛犬

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊

您可以將文件的第一行小寫,然后再傳遞給DictReader:


import csv

import itertools


def lower_first(iterator):

    return itertools.chain([next(iterator).lower()], iterator)


with open(ifs_data_file, 'rU') as datafile:

    csvDict = csv.DictReader(lower_first(datafile))

    for row in csvDict:

        print row    


查看完整回答
反對(duì) 回復(fù) 2021-03-30
?
慕的地10843

TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超8個(gè)贊

DictReader是一個(gè)老式的對(duì)象,因此super()在這里根本無法使用。您需要直接訪問property父類中的對(duì)象。在Python 2中,您要覆蓋.next()方法,而不是.__next__():


class DictReaderInsensitive(csv.DictReader):

    # This class overrides the csv.fieldnames property.

    # All fieldnames are without white space and in lower case


    @property

    def fieldnames(self):

        return [field.strip().lower() for field in csv.DictReader.fieldnames.fget(self)]


    def next(self):

        return DictInsensitive(csv.DictReader.next(self))

演示:


>>> example = '''\

... foo,Bar,BAZ

... 42,3.14159,Hello world!'''.splitlines()

>>> csvDict = DictReaderInsensitive(example)

>>> row = next(csvDict)

>>> print row

{'bar': '3.14159', 'foo': '42', 'baz': 'Hello world!'}

>>> row['BAZ']

'Hello world!'


查看完整回答
反對(duì) 回復(fù) 2021-03-30
?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

對(duì)于更簡單的方法,您可以在訪問字典之前簡單地更新DictReader.fieldnames屬性,如下所示:


>>> f = open('example-x-y-time.csv', 'rb')

>>> reader = csv.DictReader(f)

>>> reader.fieldnames

['Latitude', 'Longitude', 'Date']

>>> print next(reader)

{'Latitude': '44.8982391', 'Date': '2004-07-12', 'Longitude': '-117.7791061'}

>>> reader.fieldnames = [name.lower() for name in reader.fieldnames]

>>> print next(reader)

{'latitude': '44.6637001', 'date': '1964-04-03', 'longitude': '-123.5997009'}


查看完整回答
反對(duì) 回復(fù) 2021-03-30
  • 3 回答
  • 0 關(guān)注
  • 393 瀏覽
慕課專欄
更多

添加回答

了解更多

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)