简体   繁体   中英

Working around iteration TypeError in python

I've made a few text files that are formatted in the following way:

['number', 'number', 'number']
number
number
number

However, some of my files only contain this:

['number', 'number', 'number']

When I run my current code (seen below) and it encounters files like these, it gives me this error:

Traceback (most recent call last):
  File "product_1digcon1.py", line 879, in <module>
    punch_it()
  File "product_1digcon1.py", line 875, in punch_it
    last()
  File "product_1digcon1.py", line 742, in last
    finale()
  File "product_1digcon1.py", line 775, in finale
    for value, line in zip(values, fh):
TypeError: zip argument #1 must support iteration

I'm trying to modify my code such that, if zip argument #1 does not support iteration, the code should move on to the next file. This is what I've written so far.

def StDev():
    for root, dirs, files in os.walk('/Users/Bashe/Desktop/12/'):
        file = os.path.join(root, "Graph_StDev.txt")
        file_name_out = os.path.join(root,"StDev.txt")
        test = []
        if os.path.exists(os.path.join(root,"Graph_StDev.txt")):
            with open(file) as th, open(file_name_out,"w") as fh_out:
                    first_line = th.readline()
                    values = eval(first_line)
                        test.append(values)
                        test1 = str(test)
                        try:
                        float(test1)
                        if True:
                            for value, line in zip(values, th):
                                first_column = line.strip()
                                fh_out.write("%s\n" % (first_column))
                        except ValueError:
                            pass

Any Help would be appreciated.

You can check if the object supports iteration by using the hasattr function, which will check for the magic method iter . Basically, it will return a bool about whether or not the object supports iteration, so if it does not, you will not iterate it. Here is an example of your code, modified to not do anything to an iterable:

if hasattr(values,"__iter__"):                   #this checks if values is iterable
    for value, line in zip(values, th):
        first_column = line.strip()
        fh_out.write("%s\n" % (first_column))
else:
    pass    #if you want to do anything if the object does not support iteration, do it here

This should be what you want, and it is cleaner and more concise than exceptions handling.

Instead of if True try:

if values and th:
    for value, line in zip(values, th):
        first_column = line.strip()
        fh_out.write("%s\n" % (first_column))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM