将日期字符串拆分为可变字符
我需要在日期字符串中搜索字符,以便可以拆分字符串。
日期= '02/03/2010'
日期分离器可以是('/',',',','。')
# 回答1
选择 | 换行 | 行号
- >>> possible_characters = ('/', ' ', '-', '.')
- >>> date = '02/03/2010'
- >>> re.split("[%s]" % ("".join(possible_characters)), date)
- ['02', '03', '2010']
- >>>
# 回答2
最初的结果是我要寻找的内容,尤其是当它起作用的情况下,字符的混合物将原始字符串中的日期分开。 IE。日期= '02/03-2010'
>>> findchar =('/','',',','。',',')
>>> date = '02/03/2010'
>>> re.split(" [%s]"%("" .join(findchar)),日期)
Trackback(最近的最新电话):
文件"",第1行,在
re.split(" [%s]"%("" .join(findchar)),日期)
名称:名称" re"未定义
更改为迄今为止
>>> date.split('[%s]'%(''.join(findchar))
Trackback(最近的最新电话):
文件"",第1行,在
date.split('[%s]'%(''.join(findchar)),日期)
TypeError:需要一个整数
最初的结果是我要寻找的内容,尤其是当它起作用的情况下,字符的混合物将原始字符串中的日期分开。 IE。日期= '02/03-2010'
>>> findchar =('/','',',','。',',')
>>> date = '02/03/2010'
>>> re.split(" [%s]"%("" .join(findchar)),日期)
Trackback(最近的最新电话):
文件"",第1行,在
re.split(" [%s]"%("" .join(findchar)),日期)
名称:名称" re"未定义
更改为迄今为止
>>> date.split('[%s]'%(''.join(findchar))
Trackback(最近的最新电话):
文件"",第1行,在
date.split('[%s]'%(''.join(findchar)),日期)
TypeError:需要一个整数
# 回答3
我遗漏了一条关键的代码线:
选择 | 换行 | 行号
- import re
模块
回覆
不是内置的。
# 回答4
感谢您的帮助,它运作良好。您能解释Re.split代码线的详细信息吗?我了解发生的事情的一半。我阅读了帮助(RE),但仍在挠头,想知道它是如何工作的。
谢谢
感谢您的帮助,它运作良好。您能解释Re.split代码线的详细信息吗?我了解发生的事情的一半。我阅读了帮助(RE),但仍在挠头,想知道它是如何工作的。
谢谢
# 回答5
是否可以使用''作为日期分离器之一?
当我将逗号添加到我的分离器列表中时,我会发现一个错误。没有列表中的逗号,程序正常工作。如果我从python shell中运行dateParts线,则可以将逗号分开。
Trackback(最近的最新电话):
文件" f:\ csis 152 \ project 9 \ david peterson ex7_11.py",第112行,in
今天=日期(日期)
文件" F:\ CSIS 152 \ Project 9 \ David Peterson ex7_11.py",第20行,in __init__
self._date = self.convertDate()#convert日期到适当的格式
文件" F:\ CSIS 152 \ Project 9 \ David Peterson ex7_11.py",第31行,convertdate
parts = self.dateParts()
文件" F:\ CSIS 152 \ Project 9 \ David Peterson ex7_11.py",第40行,在dateParts中
dateParts = re.split(" [%s]"%(''.join(saparators)),self._date)#import re wor this命令工作
文件" c:\ python27 \ lib \ re.py",第167行,在拆分中
返回_compile(模式,标志).split(字符串,maxsplit)
文件" c:\ python27 \ lib \ re.py",第245行,in _compile
提出错误,v#无效的表达
错误:不良字符范围
是否可以使用''作为日期分离器之一?
当我将逗号添加到我的分离器列表中时,我会发现一个错误。没有列表中的逗号,程序正常工作。如果我从python shell中运行dateParts线,则可以将逗号分开。
Trackback(最近的最新电话):
文件" f:\ csis 152 \ project 9 \ david peterson ex7_11.py",第112行,in
今天=日期(日期)
文件" F:\ CSIS 152 \ Project 9 \ David Peterson ex7_11.py",第20行,in __init__
self._date = self.convertDate()#convert日期到适当的格式
文件" F:\ CSIS 152 \ Project 9 \ David Peterson ex7_11.py",第31行,convertdate
parts = self.dateParts()
文件" F:\ CSIS 152 \ Project 9 \ David Peterson ex7_11.py",第40行,在dateParts中
dateParts = re.split(" [%s]"%(''.join(saparators)),self._date)#import re wor this命令工作
文件" c:\ python27 \ lib \ re.py",第167行,在拆分中
返回_compile(模式,标志).split(字符串,maxsplit)
文件" c:\ python27 \ lib \ re.py",第245行,in _compile
提出错误,v#无效的表达
错误:不良字符范围
选择 | 换行 | 行号
- class Date:
- """Class for storing dates"""
- MONTHNAMES = ['January', 'February', 'March', 'April',
- 'May', 'June', 'July', 'August', 'September', 'October',
- 'November', 'December']
- MONTHLENGTHS = ['31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31']
- def __init__(self, date):
- '''Constructor'''
- self._date = date
- self._date = self.convertDate() #convert the date to the proper format
- def __str__(self):
- '''Display the date in Month XX, YYYY format'''
- #pass
- return self._date
- def convertDate(self):
- '''Converts the given date into format Month XX, YYYY format'''
- #pass
- parts = self.dateParts()
- if not parts[0] in Date.MONTHNAMES:
- parts[0] = Date.MONTHNAMES[int(parts[0])-1]
- date = parts[0] + ' ' + parts[1] + ', ' + parts[2]
- return date
- def dateParts(self):
- '''Separates the date into parts: month, day, and year.'''
- separators = ('/', ' ', '.', '-', ',')
- dateParts = re.split("[%s]" % (''.join(separators)),self._date) #import re for this command to work
- return dateParts
# 回答6
RE模块用于在字符串中执行常规表达模式匹配和替换。而不是我试图进一步解释,而是看
本教程
。
重新排序分离器似乎有效。我不知道为什么。
记住使用代码标签!
选择 | 换行 | 行号
- separators = (',', '/', ' ', '.', '-')
标签: python