Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python UDF中的字符串切片_Python_Arrays_String_Python 2.x_Udf - Fatal编程技术网

python UDF中的字符串切片

python UDF中的字符串切片,python,arrays,string,python-2.x,udf,Python,Arrays,String,Python 2.x,Udf,我正在尝试用python编写一个UDF,它将从pig脚本调用。UDF需要接受日期作为DD-MMM-YYYY格式的字符串,并返回DD-MM-YYYY格式。在这里,嗯,就像一月,二月。。DEC和返回MM将分别为01,02。。。十二, 下面是我的python UDF #!/usr/bin/python @outputSchema("newdate:chararray") def GetMonthMM(inputString): print inputString #monthstri

我正在尝试用python编写一个UDF,它将从pig脚本调用。UDF需要接受日期作为DD-MMM-YYYY格式的字符串,并返回DD-MM-YYYY格式。在这里,嗯,就像一月,二月。。DEC和返回MM将分别为01,02。。。十二,

下面是我的python UDF

#!/usr/bin/python

@outputSchema("newdate:chararray")
def GetMonthMM(inputString):
    print inputString
    #monthstring = inputString[3:6]
    sl = slice(3,6)
    monthstring = inputString[sl]
    monthdigit = ""

    if ( monthstring == "JAN" ):
        monthdigit = "01"
    elif ( monthstring == "FEB"):
        monthdigit = "02"
    elif(monthstring == "MAR"):
        monthdigit = "03"
    elif(monthstring == "APR"):
        monthdigit = "04"
    elif(monthstring == "MAY"):
        monthdigit = "05"
    elif (monthstring == "JUN"):
        monthdigit = "06"
    elif (monthstring == "JUL"):
        monthdigit = "07"
    elif (monthstring == "AUG"):
        monthdigit = "08"
    elif (monthstring == "SEP"):
        monthdigit = "09"
    elif (monthstring == "OCT"):
        monthdigit = "10"
    elif (monthstring == "NOV"):
        monthdigit = "11"
    elif (monthstring == "DEC"):
        monthdigit = "12"

    sl1 = slice(0,3)
    sl2 = slice(6,11)
    str1 = inputString[sl1]
    str2 = inputString[sl2]

    newdate = str1 + monthdigit + str2
    return monthstring;
我做了一些调试,问题似乎是在切片之后,字符串被当作数组处理。我收到以下错误消息

TypeError: unsupported operand type(s) for +: 'array.array' and 'str'
即使将该字符串与另一个字符串(如if(monthstring==“DEC”):)进行比较,也会发生同样的情况。 即使monthstring的值为DEC,条件也永远不会满足


以前有人遇到过同样的问题吗?任何关于如何修复此问题的想法。

我会将此函数编写为:

#!/usr/bin/python
@outputSchema("newdate:chararray")
def GetMonthMM(inputString):
    monthArray = {'JAN':'01','FEB':'02','MAR':'03','APR':'04','MAY':'05','JUN':'06','JUL':'07','AUG':'08','SEP':'09','OCT':'10','NOV':'11','DEC':'12'}
    print inputString
    #monthstring = inputString[3:6]
    dateparts = string.join(inputString).split('-') #assuming the date is always separated by -
    dateparts[1] = monthArray[dateparts[1]]
    return dateparts.join('-');

最近我使用了
日历
模块,在不同的情况下可能更有用,但无论如何

import calendar
m_dict = {}
for i, month in enumerate(calendar.month_abbr[1:]): #for some reason month_abbr[0] = '', so ommit that
    m_dict[month.lower()] = '{:02}'.format(i+1)

def GetMonthMM(inputStr):
    day, month, year = inputStr.split('-')
    return '-'.join([day, m_dict[month.lower()], year])

print(GetMonthMM('01-JAN-2015'))
# prints 01-01-2015

旁注:为什么不使用包含成对对象的
dict
对象
“Jan”:“01”
而不是此
elif
林。使用
calendar
模块创建此
dict
可能很容易。无法复制-一旦我将
return monthstring
替换为
return newdate
,您的代码在Python 2.7.10上对我来说运行良好。还有,哪一行给出了错误?请编辑您的问题并在错误行旁边标记
。谢谢似乎在python 2.7下工作。我已经在IPython笔记本中使用上面提到的@cxw change测试了这段代码,使用python 3,它可以工作。在python中独立执行时,代码工作正常。当我在pig脚本中将函数注册为UDF并从pig脚本中传递日期时,出现错误。拆分函数的代码失败,错误消息为:AttributeError:'array.array'对象没有属性'split'。预期的输入应该是字符串,而不是数组。。。如果输入是一个数组。然后需要修改这个行。我已经修复了代码,认为你的输入字符串是一个数组。谢谢你,沃尔特。我想这就是问题所在。以前的代码在pythonshell中独立执行时工作良好。但是当我从pig脚本调用它时,Jython解释器出于某种原因将字符串视为数组。