Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.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 更改字符串的时间格式_Python_Python 2.7 - Fatal编程技术网

Python 更改字符串的时间格式

Python 更改字符串的时间格式,python,python-2.7,Python,Python 2.7,我想写一个函数来改变时间格式,用偏移量来改变日期 例如,我有一个字符串 "this is a string 2012-04-12 23:55 with a 2013-09-12 timezone" 我想把它改成 "**this is a string 20-Apr-2012 13:40 with a 19-Sep-2013 timezone**" 也就是说,数据的格式从yyyy-mm-dd更改为dd-bbb-yyy,并且日期按偏移量移动 我编写了以下函数,但它只给出“这是一个字符串,2012

我想写一个函数来改变时间格式,用偏移量来改变日期

例如,我有一个字符串

"this is a string 2012-04-12 23:55 with a 2013-09-12 timezone"
我想把它改成

"**this is a string 20-Apr-2012 13:40 with a 19-Sep-2013 timezone**"
也就是说,数据的格式从
yyyy-mm-dd
更改为
dd-bbb-yyy
,并且日期按偏移量移动

我编写了以下函数,但它只给出“这是一个字符串,2012年6月20日13:40,时区为2013年11月12日”


有人能帮助修复/改进我的代码吗?

您的错误是由于您试图调用
report.groups()
;您从未对
报表
参数应用过正则表达式

您的代码可以大大简化:

_dt = re.compile(r"""
    [12]\d{3}-   # Year (1xxx or 2xxx),
    [0-1]\d-     # month (0x, 1x or 2x)
    [0-3]\d      # day (0x, 1x, 2x or 3x)
    (?:\s        # optional: time after a space
        [0-2]\d: # Hour (0x, 1x or 2x)
        [0-5]\d  # Minute (0x though to 5x)
    )?
    """, flags=re.VERBOSE)

def _deIDReportDate(report, offset=654321):
    def replace(match):
        dt = match.group()

        if len(dt) > 10:
            # with time
            dt = datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M')
            dt += datetime.timedelta(seconds=offset)
            return dt.strftime('%d-%b-%Y %H:%M')

        dt = datetime.datetime.strptime(dt, '%Y-%m-%d')
        dt += datetime.timedelta(seconds=offset)
        return dt.strftime('%d-%b-%Y')

    return _dt.sub(replace, report)

为输入字符串中的每个非重叠匹配调用
replace
嵌套函数。

“但是有错误”……您介意详细说明一下吗?错误是什么?当前结果是什么?错误消息:“str”对象没有属性“groups”
report
是一个字符串。它没有
groups
方法,因此
report.groups()
不起作用。我如何更改它使其起作用?也许应该用VERBOSE分隔正则表达式?@AaronHall:有那么长吗?也许我可以把它拆分一下。非常感谢!我可以问你一个问题吗?为什么在“如果len(dt)>10”中指定“10?”@neymar:日期部分有10个字符长;4位数字表示年份,2位数字表示月份,2位数字表示日期,加上2个破折号等于10。谢谢!我感谢你的帮助
_dt = re.compile(r"""
    [12]\d{3}-   # Year (1xxx or 2xxx),
    [0-1]\d-     # month (0x, 1x or 2x)
    [0-3]\d      # day (0x, 1x, 2x or 3x)
    (?:\s        # optional: time after a space
        [0-2]\d: # Hour (0x, 1x or 2x)
        [0-5]\d  # Minute (0x though to 5x)
    )?
    """, flags=re.VERBOSE)

def _deIDReportDate(report, offset=654321):
    def replace(match):
        dt = match.group()

        if len(dt) > 10:
            # with time
            dt = datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M')
            dt += datetime.timedelta(seconds=offset)
            return dt.strftime('%d-%b-%Y %H:%M')

        dt = datetime.datetime.strptime(dt, '%Y-%m-%d')
        dt += datetime.timedelta(seconds=offset)
        return dt.strftime('%d-%b-%Y')

    return _dt.sub(replace, report)