如何用替换列标题列表替换字符串中的列标题?

2024-05-29 02:20:33 发布

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

我正在尝试在Power Query中格式化数据集。我的想法是从组织列的Power查询中获取公式,并使用正则表达式替换方法替换新的列标题,以便正确格式化数据集

Excel公式=Table.TransformColumnTypes(#“提升的标题”、{{{“ID_ORD”、type text}、{“CDRNOR”、type text}、{“DT_ORD”、type datetime}、{“FLSTAT”、Int64.type}

引号内的文本是我要替换的文本,除了第一个“提升的标题”

下面是新列的列表:CDUNOD CDTIDO CDRNOR DT_ORD DS_ORD FLNOEX…等等

到目前为止,我的想法如下:

import re
oldcolumn = '''Table.TransformColumnTypes(#"Promoted Headers",{{"ID_ORD", type text}, {"CDRNOR", type text}, {"DT_ORD", type datetime}, {"FLSTAT", Int64.Type},.....'''
newcolumn = '''CDUNOD   CDTIDO  CDRNOR  DT_ORD  DS_ORD  FLNOEX....'''
newcolumnList = newcolumn.split()
originalcolumnsRegex = re.compile(r'\{\"\w+')

我不知道如何迭代字符串并用列表newcolumnList的内容替换每个匹配项

我猜应该是这样的

for i in newcolumnList
      originalcolumnsRegex.sub(r'{"'newcolumnList[i],oldcolumn)
      i = i +1

基本上,我希望获得一个字符串输出,并在Power Query formula bar中使用新的标题代替旧的标题

应该知道,我对Python和一般编程非常陌生


Tags: 数据textid标题datetimetypedttable
1条回答
网友
1楼 · 发布于 2024-05-29 02:20:33

我想这可能行得通。(我从newcolumn字符串中取了几个值来匹配数字,只取了一个平头。)

import re
oldcolumn = '''Table.TransformColumnTypes(#"Promoted Headers",{{"ID_ORD", type text}, {"CDRNOR", type text}, {"DT_ORD", type datetime}, {"FLSTAT", Int64.Type},.....'''
newcolumn = '''CDUNOD   CDTIDO  CDRNOR  DT_ORD....'''
newcolumnList = newcolumn.split()

# Compiled pattern
ptrn = r'{"([A-Z_]+?)"'
p = re.compile(ptrn)

# You can adjust, but we're just zipping together the old and new values
# using `re.findall()`, unpacking each, and setting the old string to 
# a "new" string with the replacement values.

for o, n in zip(p.findall(oldcolumn), newcolumnList):
    oldcolumn = oldcolumn.replace(o, n)

输出:

 'Table.TransformColumnTypes(#"Promoted Headers",{{"CDUNOD", type text}, {"CDTIDO", type text}, {"CDRNOR", type datetime}, {"DT_ORD....", Int64.Type},.....'

相关问题 更多 >

    热门问题