Geo*_*Com 6 python regex pandas
我有一个文本文件,其中包含大量文件路径file.txt:
C:\data\AS\WO\AS_WOP_1PPPPPP20070506.bin
C:\data\AS\WO\AS_WOP_1PPPPPP20070606.bin
C:\data\AS\WO\AS_WOP_1PPPPPP20070708.bin
C:\data\AS\WO\AS_WOP_1PPPPPP20070808.bin
...
Run Code Online (Sandbox Code Playgroud)
我用Regex从路径中提取日期的方法:
import re
textfile = open('file.txt', 'r')
filetext = textfile.read()
textfile.close()
data = []
for line in filetext:
matches = re.search("AS_[A-Z]{3}_(.{7})([0-9]{4})([0-9]{2})([0-9]{2})", line)
data.append(line)
Run Code Online (Sandbox Code Playgroud)
它没有给我想要的东西.
我的输出应该是这样的:
year month
2007 05
2007 06
2007 07
2007 08
Run Code Online (Sandbox Code Playgroud)
然后将其保存为列表列表:
[['2007', '5'], ['2007', '6'], ['2007', '7'], ['2007', '8']]
Run Code Online (Sandbox Code Playgroud)
或将其保存为熊猫系列.
有什么方法regex可以得到我想要的东西!?
使用 pandas 试试这个:
df = pd.read_csv('yourfile.txt',header=None)
df.columns = ['paths']
# pandas string method extract takes a regex
df['paths'].str.extract('(\d{4})(\d{2})')
Run Code Online (Sandbox Code Playgroud)
输出:
0 1
0 2007 05
1 2007 06
2 2007 07
3 2007 08
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
655 次 |
| 最近记录: |