Home > other >  Python TypeError: object is not iterable
Python TypeError: object is not iterable

Time:10-29

I am still new with python and I am having to create a method that reads some txt files and returns a list for each file with the values of a specific column.

I tried to create an array with all the files I have to read and send this as a parameter to my method.

import json, requests, urllib.parse, re
from pandas.io.parsers import read_csv
import pandas as pd
from termcolor import colored
import numpy as np
from glob import glob
import os


# Set Up
dateinplay = "2021-09-27"
cdwenv1 = "cdwu"  # Note that it only works with the http version right now
cdwenv2 = "cdwp"  # Control Env, usually cdwp
CoreMurexFilesLoc = r"J:\Gerard\Release197\UAT\Files\Core"

# Dev Static
cdwenv = ""  # leave empty
files = glob(CoreMurexFilesLoc   "\\*")

# index 1: MHEU_TradeCash_ | 2: TradeCash_
tradeCashfiles = [i for i in files if "TradeCash" in i]

# index 1: MHEU_Trade_ | 2: Trade_
tradeFiles = [i for i in files if "Trade_" in i]

mheu_tradeCash = tradeCashfiles[1]
tradeCash = tradeCashfiles[2]
mheu_trade = tradeFiles[1]
trade = tradeFiles[2]

filesList = [mheu_trade, trade, mheu_tradeCash, tradeCash]


def read_csv(input: list):
    for file in list:
        df_trade = pd.read_csv(
            file,
            delimiter="|",
            index_col=False,
            dtype="unicode",
        )

        # Drop any blank fields
        df_trade.dropna(subset=["MurexCounterpartyRef"], inplace=True)
        tradeList = df_trade["MurexCounterpartyRef"]

    return tradeList


read_csv(filesList)

But I am getting a non-iterable object error, I don't understand why I can't interact over this list.

Traceback (most recent call last):
  File "h:\DESKTOP\test_check\ultimateParent.py", line 50, in <module>
    read_csv(filesList)
  File "h:\DESKTOP\test_check\ultimateParent.py", line 35, in read_csv
    for file in list:
TypeError: 'type' object is not iterable

If someone can help me identify and solve the error, I would appreciate it.

CodePudding user response:

This is happening because you're trying to iterate python's built-in derived data type namely 'list'.
According to what I've understood, try replacing 'list' with 'Input'

def read_csv(myInput: list):
    for file in myInput:
        df_trade = pd.read_csv(
            file,
            delimiter="|",
            index_col=False,
            dtype="unicode",
        )

        # Drop any blank fields
        df_trade.dropna(subset=["MurexCounterpartyRef"], inplace=True)
        tradeList = df_trade["MurexCounterpartyRef"]

    return tradeList

Maybe you wrote 'list' instead of 'input' inside the function.

Side Note: Generally avoid using python's reserved words, (here 'input') as they can lead to ambiguity, and in worst case, run-time errors.

  • Related