Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.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_String_Parsing_Text_Methods - Fatal编程技术网

如何在Python中重新格式化时间戳

如何在Python中重新格式化时间戳,python,string,parsing,text,methods,Python,String,Parsing,Text,Methods,我下面有一些文本需要处理。时间戳当前已列出,然后是值。时间戳的格式是yyyyymmdd,我希望能够将其更改为yyyy-mm-dd或其他一些变体:yyyy/mm/dd,等等。我似乎找不到将字符插入字符串的字符串方法,所以我不确定最好的方法。在这里寻找关于python文本切片和切分的效率和一般建议。提前谢谢 19800101,0.76 19800102,0.00 19800103,0.51 19800104,0.00 19800105,1.52 19800106,2.54 19800107,0.00

我下面有一些文本需要处理。时间戳当前已列出,然后是值。时间戳的格式是
yyyyymmdd
,我希望能够将其更改为
yyyy-mm-dd
或其他一些变体:
yyyy/mm/dd
,等等。我似乎找不到将字符插入字符串的字符串方法,所以我不确定最好的方法。在这里寻找关于python文本切片和切分的效率和一般建议。提前谢谢

19800101,0.76
19800102,0.00
19800103,0.51
19800104,0.00
19800105,1.52
19800106,2.54
19800107,0.00
19800108,0.00
19800109,0.00
19800110,0.76
19800111,0.25
19800112,0.00
19800113,6.10
19800114,0.00
19800115,0.00
19800116,2.03
19800117,0.00
19800118,0.00
19800119,0.25
19800120,0.25
19800121,0.00
19800122,0.00
19800123,0.00
19800124,0.00
19800125,0.00
19800126,0.00
19800127,0.00
19800128,0.00
19800129,0.00
19800130,7.11
19800131,0.25
19800201,.510
19800202,0.00
19800203,0.00
19800204,0.00
python中文本切片和切分的一般建议

切片操作符:

str = '19800101,0.76'
print('{0}-{1}-{2}'.format(str[:4], str[4:6], str[6:]))
阅读:(在切片上查找零件),然后

python中文本切片和切分的一般建议

切片操作符:

str = '19800101,0.76'
print('{0}-{1}-{2}'.format(str[:4], str[4:6], str[6:]))

阅读:(查找切片上的部分)和。

字符串是不可变的,因此在字符串中插入字符将不起作用。试试这个:

date = '19800131'
print '-'.join([date[:4],date[4:6],date[6:]])

字符串是不可变的,因此在字符串中插入字符将不起作用。试试这个:

date = '19800131'
print '-'.join([date[:4],date[4:6],date[6:]])

我会这样做:

#!/usr/bin/env python

from datetime import datetime

with open("stuff.txt", "r") as f:
    for line in f:
        # Remove initial or ending whitespace (like line endings)
        line = line.strip()

        # Split the timestamp and value
        raw_timestamp, value = line.split(",")

        # Make the timestamp an actual datetime object
        timestamp = datetime.strptime(raw_timestamp, "%Y%m%d")

        # Print the timestamp separated by -'s. Replace - with / or whatever.
        print("%s,%s" % (timestamp.strftime("%Y-%m-%d"), value))

这允许您使用允许的任何格式导入或打印时间戳。

我会这样做:

#!/usr/bin/env python

from datetime import datetime

with open("stuff.txt", "r") as f:
    for line in f:
        # Remove initial or ending whitespace (like line endings)
        line = line.strip()

        # Split the timestamp and value
        raw_timestamp, value = line.split(",")

        # Make the timestamp an actual datetime object
        timestamp = datetime.strptime(raw_timestamp, "%Y%m%d")

        # Print the timestamp separated by -'s. Replace - with / or whatever.
        print("%s,%s" % (timestamp.strftime("%Y-%m-%d"), value))

这允许您使用允许的任何格式导入或打印时间戳。

谢谢!我如何有效地使用它,读取文件(有许多条目)并循环只更改时间戳,而不更改值(逗号后)?谢谢!我如何有效地使用它,读取文件(有许多条目)并循环只更改时间戳,而不更改值(逗号后)?谢谢!我如何有效地使用它,读取文件(有许多条目)并只循环更改时间戳,而不使用值(逗号后)?@Yuri读取整个文件并遍历每一行(例如使用),对其应用format函数。很抱歉,我仍然不清楚。readlines()似乎将所有内容都放在一个列表中,而该列表不能由字符串格式处理。对不起,我是个讨厌的人,你能举个例子吗?我试过一些方法,但都没有成功。谢谢,谢谢!我如何有效地使用它,读取文件(有许多条目)并只循环更改时间戳,而不使用值(逗号后)?@Yuri读取整个文件并遍历每一行(例如使用),对其应用format函数。很抱歉,我仍然不清楚。readlines()似乎将所有内容都放在一个列表中,而该列表不能由字符串格式处理。对不起,我是个讨厌的人,你能举个例子吗?我试过一些方法,但都没有成功。谢谢