python - 如何在连字符之前获取字符串

标签 python

我有以下文件名:

pagecounts-20150802-000000

我想从上面的 20150802 中提取日期 我正在使用以下代码,但它不起作用:

print os.path.splitext("pagecounts-20150802-000000")[0]

最佳答案

os.path 中的方法主要用于路径字符串操作。你想使用字符串拆分:

print 'pagecounts-20150802-000000'.split('-')[1]

关于python - 如何在连字符之前获取字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32682199/

相关文章:

android - NFCPY : Safely exiting beam. py 示例

Python Pandas : convert a column to a string?

python try except 作为一个函数来评估表达式

Python:不工作 StatsModels

python - TinyCSS 和 GTK 主题 @define-color

python - 在 Python 中分配空值或字符串

python - 使用 Pygame 定位移动图像/矩形

python - 如何在 Python 中将 Excel 工作表复制到另一个工作簿

python - 使用 PyCharm(或任何其他 IDE)分析 python 程序

python - 假设列表旁边的所有其他数字都是其值,如何表示列表中的字典?