Python:将字段值提取到新列中,写入Excel

2024-05-20 00:54:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在读取CSV文件,并打算写入Excel文件。CSV文件只有两列,但是在写入Excel之前,我想使用regex提取列数据并创建新列

CSV文件:test.csv

name, file_info
test, c:\folder1\subfolder1\subfolder2\example.xls | history 12345 at 2020-01-01

以下是我目前掌握的代码:

import csv

with open('test.csv',mode='r') as testFile
     reader = csv.DictReader(testFile, delimiter=',')
     for row in reader:
          ### This is where i assume i need to perform the regex operation on the current row

我想将文件名(example.xlsx)、历史记录(12345)和日期(2020-01-01)提取为Excel文件中的列

我成功地测试了正则表达式

"\\([^\\|]*)\s*\|\"

我相信有多种方法可以做到这一点。熊猫会更好吗?我可以通过以下方式简单地将文件读写到excel:

df = pd.read_csv('test.csv')
df.to_excel('text.xlsx)

我没有任何熊猫的经验,所以不知道如何使用regex执行我想要的操作,并将其连接在一起

最终产品是具有五(5)列的excel电子表格

name | path | file | history | date


Tags: 文件csvtonametestexampleexcelhistory
1条回答
网友
1楼 · 发布于 2024-05-20 00:54:42

下面是一种使用Pandasdf['column'].str.extract()函数的技术

您可以将已编译(或未编译)的正则表达式字符串传递到extract()函数中。这将使用表达式中的命名组,并将这些组提取到具有相同名称的列中

样本数据:

name,file_info
test1,c:\folder1\subfolder1\subfolder2\example1.xls | history 12345 at 2020-01-01
test2,c:\folder1\subfolder1\subfolder2\example2.xls | history 24687 at 2020-01-12
test3,c:\folder1\subfolder1\subfolder2\example3.xls | history 33445 at 2020-01-13
test4,c:\folder1\subfolder1\subfolder2\example4.xls | history 55664 at 2020-01-14

代码:

import os
import pandas as pd
import re

# Define constants
COLS = ['name', 'path', 'file', 'history', 'date']
PATH = './test.csv'
PATH_XL = './test.xlsx'
RE_EXP = re.compile(r'^'
                    '(?P<path>.*)\|\shistory\s'
                    '(?P<history>\d+)\sat\s'
                    '(?P<date>\d{4}-\d{2}-\d{2})$',
                    re.IGNORECASE)

# Read CSV file.
df = pd.read_csv(PATH)
# Create new columns using named regex groups.
df[['path', 'history', 'date']] = df['file_info'].str.extract(RE_EXP)
# Extract the filename from the path using a built-in function.
df['file'] = df['path'].apply(os.path.basename)
# Convert date to datetime format.
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d').dt.date
# Subset DataFrame to only the columns we require.
df = df[COLS]
# Write results to Excel.
df.to_excel(PATH_XL, index=False)

Excel输出:

enter image description here

相关问题 更多 >