python - 使用 python pandas 对列进行正则表达式搜索/替换
问题描述
以下是我尝试对其进行一些数据操作的 .csv 文件的一个小示例。每个“comment”列都有自己的列,由半冒号(“date;user;comment”)分隔。我的目标是在用户前面加上“gp-”
原来的:
issue_key,summary,comment,comment,comment,comment,resolution
ABC-1234,summary1,"03/11/2021 12:18;user1;a text comment","03/10/2021 11:18;user2,a text comment",,,Unresolved
ABC-4321,summary2,"03/08/2021 12:10;user7;a text comment","03/10/2021 11:18;user5,a text comment",,,Unresolved
ABC-2214,summary3,"03/09/2021 12:20;user9;a text comment",,"03/10/2021 11:18;user3,a text comment",,Unresolved
我希望将其转换为:
issue_key,summary,comment,comment,comment,comment,resolution
ABC-1234,summary1,"03/11/2021 12:18;gp-user1;a text comment","03/10/2021 11:18;gp-user2,a text comment",,,Unresolved
ABC-4321,summary2,"03/08/2021 12:10;gp-user7;a text comment","03/10/2021 11:18;gp-user5,a text comment",,,Unresolved
ABC-2214,summary3,"03/09/2021 12:20;gp-user9;a text comment",,"03/10/2021 11:18;gp-user3,a text comment",,Unresolved
我到目前为止的代码。我想我很接近:
with open(destination_filename) as f:
orig_header = f.readline()
orig_header = orig_header.split(",")
orig_header[-1] = orig_header[-1].strip()
csv_data = pd.read_csv(destination_filename)
cols = csv_data.columns[csv_data.columns.str[:7]=='Comment']
csv_data[cols] = csv_data[cols].apply(lambda x: re.sub(r'(\d+\/\d+\/\d\d\d\d \d+:\d+);(\S+);(.*)', r'\1;gp-\2;\3', str(x)))
csv_data.to_csv(f"{destination_filename}", index = False, header=orig_header)
解决方案
一种方法是使用内置csv
库。它还可用于将注释字段处理为;
单独的 csv 行。
例如:
import io
import csv
def replace_user(entry):
if len(entry):
values = next(csv.reader(io.StringIO(entry, newline=''), delimiter=';'))
values[1] = f'gp-{values[1]}'
entry = ';'.join(values)
return entry
with open('input.csv', newline='') as f_input, open('output.csv', 'w', newline='') as f_output:
csv_input = csv.reader(f_input)
csv_output = csv.writer(f_output)
csv_output.writerow(next(csv_input)) # copy the header
for row in csv_input:
row[2:6] = [replace_user(v) for v in row[2:6]]
csv_output.writerow(row)
给你一个output.csv
包含:
issue_key,summary,comment,comment,comment,comment,resolution
ABC-1234,summary1,03/11/2021 12:18;gp-user1;a text comment,"03/10/2021 11:18;gp-user2,a text comment",,,Unresolved
ABC-4321,summary2,03/08/2021 12:10;gp-user7;a text comment,"03/10/2021 11:18;gp-user5,a text comment",,,Unresolved
ABC-2214,summary3,03/09/2021 12:20;gp-user9;a text comment,,"03/10/2021 11:18;gp-user3,a text comment",,Unresolved
如果注释也可以有引号或换行符,则可以使用附加的csv.writer()
来代替join()
.
推荐阅读
- c++ - 代码块 16 位消息
- javascript - 我在函数之外提到的变量“x”我猜有一些问题。函数没有访问变量不知道为什么
- typescript - ASP.NET Core MVC + Vue + 单文件组件
- php - 在 codignator 中的日期之间获取数据
- android - 如何在 VHAL android Automotive 中处理自定义车辆属性?
- firefox-developer-tools - Firefox - 如何查看打开新窗口的网络请求
- c# - 不使用 Virtual Comport 直接向客户端发送 modbus rtu 命令
- scala - 遍历 Scala 期货的 Seq 时如何减少上下文切换
- flutter - Flutter:防止抽屉在 FutureBuilder 内滑动时始终刷新
- installation - centOS7 aarch64如何安装mono