将zip传递给函数会导致“ zip参数#2必须支持迭代”错误

时间:2019-11-11 14:25:26

标签: python arrays csv

我正在尝试将zip()传递给函数,但它给出了“ TypeError:zip参数#2必须支持迭代”我不知道是什么原因。

代码如下:

def translate(self,url,phrases):
        translator = Translator()
        translations_ar = translator.translate(phrases, dest='ar')
        translations_hy = translator.translate(phrases, dest='hy')
        translations_ms = translator.translate(phrases, dest='ms')
        translations_bg = translator.translate(phrases, dest='bg')
        # Chinese (Hong Kong SAR) changed with Chinese (Simplified) 
        translations_zh_cn_SAR = translator.translate(phrases, dest='zh-cn')
        # Chinese (Simplified)
        translations_zh_cn = translator.translate(phrases, dest='zh-cn')
        # Chinese (Singapore) changed with Chinese (Traditional)
        translations_zh_tw_singapore = translator.translate(phrases, dest='zh-tw')
        # Chinese (Traditional)
        translations_zh_tw = translator.translate(phrases, dest='zh-tw')
        translations_hr = translator.translate(phrases, dest='hr')
        translations_cs = translator.translate(phrases, dest='cs')
        translations_da = translator.translate(phrases, dest='da')
        translations_nl = translator.translate(phrases, dest='nl')
        translations_en_australia = translator.translate(phrases, dest='en')
        translations_en_uk = translator.translate(phrases, dest='en')
        translations_en_usa = translator.translate(phrases, dest='en')
        translations_et = translator.translate(phrases, dest='et')
        translations_fi = translator.translate(phrases, dest='fi')

        # Creating Array of Languages
        ar = [translation_ar.text for translation_ar in translations_ar]    
        hy = [translation_hy.text for translation_hy in translations_hy]    
        ms = [translation_ms.text for translation_ms in translations_ms]    
        bg = [translation_bg.text for translation_bg in translations_bg]
        zh_cn_SAR = [translation_zh_cn_SAR.text for translation_zh_cn_SAR in translations_zh_cn_SAR]    
        zh_cn = [translation_zh_cn.text for translation_zh_cn in translations_zh_cn]
        zh_tw_singapore = [translation_zh_tw_singapore.text for translation_zh_tw_singapore in translations_zh_tw_singapore]
        zh_tw = [translation_zh_tw.text for translation_zh_tw in translations_zh_tw]
        hr = [translation_hr.text for translation_hr in translations_hr]
        cs = [translation_cs.text for translation_cs in translations_cs]
        da = [translation_da.text for translation_da in translations_da]
        nl = [translation_nl.text for translation_nl in translations_nl]
        en_australia = [translation_en_australia.text for translation_en_australia in translations_en_australia]
        en_uk = [translation_en_uk.text for translation_en_uk in translations_en_uk]
        en_usa = [translation_en_usa.text for translation_en_usa in translations_en_usa]
        et = [translation_et.text for translation_et in translations_et]
        fi = [translation_fi.text for translation_fi in translations_fi]

        # zip arrays
        languages = zip(ar,hy,ms,bg,zh_cn_SAR,zh_cn,
        zh_tw_singapore,zh_tw,hr,cs,da,nl,en_australia,en_uk,en_usa,et,fi)   
        coderasha.write_csv(languages) 

上面的代码来自另一个函数,我必须将“语言”传递给write_csv函数:

    def write_csv(*args):
        with open('translations.csv', mode='w', newline='', encoding="utf-8") as csv_file:
            fieldnames = ['Arabic', 'Armenian', 'Malaysia', 'Bulgarian', 'Chinese (Hong Kong SAR)', 
            'Chinese (Simplified)','Chinese (Singapore)','Chinese (Traditional)','Croatian', 'Czech',
            'Danish','Dutch','English (Australia)','English (UK)', 'English (USA)','Estonian','Finnish']
            writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
            writer.writeheader()
            for lang in args:
                writer.writerow(dict(zip(fieldnames, lang)))    

请帮助我解决此问题。谢谢!

0 个答案:

没有答案