【发布时间】:2020-06-19 06:52:34
【问题描述】:
我正在尝试替换返回字符串的字符。 首先我得到字符串
import os
import re
#List .xlsx files followed by the string ESC
filenames = os.listdir('//xxx.xxx.xxx.com//a//b//c//archive')
for filename in filenames:
getdate = re.search('(?<=ESC_)\w+', filename)
print (getdate)
作为回报,我得到了这个
<re.Match object; span=(27, 34), match='2020_01'>
None
这是正确的。 我随后尝试了这个:
#Replace '_' with '-'
date = getdate.replace('_', '-')
print(date)
但它给了我一个错误
AttributeError: 'NoneType' object has no attribute 'replace'
有什么建议吗?从(getdate)返回的字符串不被识别吗?
另外,我如何将该字符串写入具有一些额外值的 .prm 文件,例如:
Body of .prm file has (date), aaaa, bbbb and cccc
【问题讨论】:
标签: python string file variables replace