.join语句中的TypeError

时间:2016-11-21 21:55:10

标签: python typeerror

我已将环境变量设置为与"PERIOD"对应并且我一直收到以下错误。目标是让代码响应env变量并使用它们来触发用户提交的数据集中的分隔符。

Traceback (most recent call last):
File "./qwikifwlistmgr", line 103, in <module>
    delim(in_list)
File "./qwikifwlistmgr", line 94, in delim
    e = ".".join(e)
TypeError

我确保所有陈述都有回报。但是现在我不确定还有什么不对。我感觉它很简单,但我无法看到它。

#! /usr/bin/env python
import os
import sys
import re
x = os.getenv("QWIKIFWLISTMGR_DELIMITER")


in_list = sys.argv



def ifw(in_list):
    usr_choice = (in_list)[1]
    if usr_choice == 'i':
        print(int_sort(in_list))
    elif usr_choice =='f':
        print(float_sort(in_list))
    elif usr_choice == 'w':
        print(word_sort(in_list))
    else:
        return in_list





def float_sort(in_list):
    float_sort = "test"
    sorted_float = "test"
    float_sort = in_list[2:]
    float_sort = ''.join(float_sort)
    #float_sort1 = " ".join(list((re.findall(r"((?<!\S)\d+(?!\S))", float_sort))))
    #float_sort2 = ' '.join(list(re.findall(r"(\d+\.\d+)", float_sort)
    float_sort = "  ".join(re.findall(r"\d*\.\d+|\d+", float_sort))
    sorted_float = sorted(float_sort, key=len)
    return float_sort


def word_sort(in_list):
     word_sort = " 1a "
     word_sort = sorted(in_list[2:], key=len) #in_list must be 2 because the program will view usr input as a word
     for i in word_sort:
         punctuation = '.',',',';','!',' / ','"','?' #strips punctuation from words
         if i in punctuation: #removes punctuation
             word_sort = word_sort.replace(i," ")
     #word_sort= sorted(word_sort, key=lambda L: (L.lower(), L))
     word_sort= " ".join(sorted(word_sort, key=lambda L: (L.lower(), L))) #takes string and sorts by length giving priority to upper over lower when tied
     sorted_word = " 1a " #left for testing
     sorted_word = re.sub("\S+\d\S+", "", word_sort).strip() #removes any word with a number in it
     sorted_word = "".join(sorted_word) #joins the results back into a string
     return sorted_word




def int_sort(in_list):
    int_sort = "3"
    in_list = " ".join(in_list[1:]) # takes information from argv and creates a string with it
    int_sort = " ".join(list(reversed(re.findall(r"(?<!\S)\d+(?!\S)", in_list))))
    # find all looks for a pattern of anything but a space... any number. anything besides a space in the in_list and returns it
    #reveresd flips that return backwards
    # list turns that into a list and join makes it a string again
    return int_sort

def delim(in_list):
    z = (ifw(in_list))
    x = "screw python"
    e = (ifw(in_list))
    x = os.getenv('QWIKIFWLISTMGR_DELIMITER')
    if x == 'BLANK':
        e = ' '.join(ifw(in_list))
        return e
    elif x =='TAB':
        e = ifw(in_list)
        e = '\t'.join(e)
        return e
    elif x =='NL':
        e = ifw(in_list)
        e = '\n'.join(e)
        return e
    elif x =='COLON':
        e = ifw(in_list)
        e = ':'.join(e)
        return e
    elif x =='COMMA':
        e = ifw(in_list)
        e = ','.join(e)
        return e
    elif x =='SEMICOLON':
        e = ifw(in_list)
        e = ';'.join(e)
        return e
    elif x =='PERIOD':
        e = ".".join(e)
        return e
    elif x =='SLASH':
        e = ifw(in_list)
        e = '/'.join(e)
        return e
    else:
        e = ifw(in_list)
        return e
delim(in_list)

1 个答案:

答案 0 :(得分:1)

问题来自ifw函数,如果除None分支之外的其他所有分支都返回else。因此,以下表达式计算并将None值赋给e

e = ifw(in_list)
...
e = ".".join(e)

您可以通过返回一个空的iterable来修复此问题,作为函数中的默认操作:

def ifw(in_list):
    if usr_choice == 'i':
        ...
    else:
        return in_list
    return []

或者在if/elif

的每个分支中返回适当的字符串列表