如何构建下表?

2024-06-16 14:38:19 发布

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

您好,我处理了一个excel文件,并将它的一些参数用于创建许多表。表的结构如下:

"AWK|USL|R|SVKDIKG_tVstiKg|S|[PARAMETER1]~BURAGO~[PARAMETER2]~WVDG~333" "AFUSLR~USLSSHS~Farm~~%ERD_ARGV=MR4567.%VRSD%.%23WF%.333.%RVB%.tRt"
"AWK|USL|R|Bimbo|S|[PARAMETER3]~K~999" "USLo99941VRR.VxV"
"AWK|USL|R|Bimbo|S|[PARAMETER3]~Q~999" "USLo99941VRR.VxV"
"AWK|USL|R|Ford|S|[PARAMETER3]~K~999" "[PARAMETER3]~K"
"AWK|USL|R|Ford|S|[PARAMETER3]~Q~999" "[PARAMETER3]~K"

创建表所需的参数包含在excel文件中,如下所示:

123123,RIBICOM,FACTIBLE
050944,TELCOM,423423
.
.
.
42342,CORPS,233243

我们的想法是将“,”作为一个列分隔符,其中第一列是“PARAMETER1”,第二列是“PARAMETER2”,最后是“PARAMETER3”,第三列是raw by raw,对于每个raw或这个归档文件,我需要生成一个表来填充模板的占位符,如下所示:

"AWK|USL|R|SVKDIKG_tVstiKg|S|123123~BURAGO~RIBICOM~WVDG~333" "AFUSLR~USLSSHS~Farm~~%ERD_ARGV=MR4567.%VRSD%.%23WF%.333.%RVB%.tRt"
"AWK|USL|R|Bimbo|S|FACTIBLE~K~999" "USLo99941VRR.VxV"
"AWK|USL|R|Bimbo|S|FACTIBLE~Q~999" "USLo99941VRR.VxV"
"AWK|USL|R|Ford|S|FACTIBLE~K~999" "FACTIBLE~K"
"AWK|USL|R|Ford|S|FACTIBLE~Q~999" "FACTIBLE~K"

更清楚的是,模板的占位符如下:

[PARAMETER1]
[PARAMETER2]
[PARAMETER3]

这些都是我需要填补的

上面的示例将是第一行所需的输出,我需要生成一个包含所有表的txt文件,为了实现这一点,我尝试了:

import pandas as pd
# -*- coding: utf-8 -*-
xl = pd.ExcelFile("Book1.xlsx")
#to clean from duplicates
df = xl.parse("Sheet1")
df=df.drop_duplicates()
#these are the values that I am concatenating below
Parameter1=df[u'Header1 ']
Parameter2=df[u'Header2 ']
Parameter3=df[u'Header3 ']

#This is the dataframe with the corresponding columns
important_Parameters=df[u'Header1 '].astype(str)+","+df[u'Header2 '].astype(str)+","+df[u'Header3 '].astype(str)

#to write my dataframe on disk.
important_Parameters.to_csv("important33.txt", index=False)

我不确定什么是最好的方法来继续,因为我以前在bash中使用“sed”和“awk”来做这类事情,但这次我想尝试使用pandas和python,我非常感谢任何建议来继续这个特定的任务。你知道吗


Tags: 文件thetodfrawuslawkford
1条回答
网友
1楼 · 发布于 2024-06-16 14:38:19

你试试这个

import pandas as pd
# -*- coding: utf-8 -*-
df = pd.read_csv("param.csv")
print df
df=df.drop_duplicates()
filename='sample.txt'

print "\n\nReplace with new values"
for index, row in df.iterrows():
    print "New Values \n\n"
    print row 
    f=open(filename)
    filedata = f.read()
    filedata=filedata.replace("[PARAMETER1]",row[0])
    filedata=filedata.replace('[PARAMETER2]',row[1])
    filedata=filedata.replace('[PARAMETER3]',row[2])
    print filedata

输出

      Parameter1 Parameter2 Parameter3
    0    123123A    RIBICOM   FACTIBLE
    1   050944BS     TELCOM     423423


    Replace with new values
    New Values


    Parameter1     123123A
    Parameter2     RIBICOM
    Parameter3    FACTIBLE
    Name: 0, dtype: object
    AWK|USL|R|SVKDIKG_tVstiKg|S|123123A~BURAGO~RIBICOM~WVDG~333 AFUSLR~USLSSHS~Farm~
    ~%ERD_ARGV=MR4567.%VRSD%.%23WF%.333.%RVB%.tRt
    AWK|USL|R|Bimbo|S|FACTIBLE~K~999 USLo99941VRR.VxV
    AWK|USL|R|Bimbo|S|FACTIBLE~Q~999 USLo99941VRR.VxV
    AWK|USL|R|Ford|S|FACTIBLE~K~999 FACTIBLE~K
    AWK|USL|R|Ford|S|FACTIBLE~Q~999 FACTIBLE~K
    New Values


    Parameter1    050944BS
    Parameter2      TELCOM
    Parameter3      423423
    Name: 1, dtype: object
    AWK|USL|R|SVKDIKG_tVstiKg|S|050944BS~BURAGO~TELCOM~WVDG~333 AFUSLR~USLSSHS~Farm~
    ~%ERD_ARGV=MR4567.%VRSD%.%23WF%.333.%RVB%.tRt
    AWK|USL|R|Bimbo|S|423423~K~999 USLo99941VRR.VxV
    AWK|USL|R|Bimbo|S|423423~Q~999 USLo99941VRR.VxV
    AWK|USL|R|Ford|S|423423~K~999 423423~K
    AWK|USL|R|Ford|S|423423~Q~999 423423~K

你知道吗示例.txt你知道吗

"AWK|USL|R|SVKDIKG_tVstiKg|S|[PARAMETER1]~BURAGO~[PARAMETER2]~WVDG~333" "AFUSLR~USLSSHS~Farm~~%ERD_ARGV=MR4567.%VRSD%.%23WF%.333.%RVB%.tRt"
"AWK|USL|R|Bimbo|S|[PARAMETER3]~K~999" "USLo99941VRR.VxV"
"AWK|USL|R|Bimbo|S|[PARAMETER3]~Q~999" "USLo99941VRR.VxV"
"AWK|USL|R|Ford|S|[PARAMETER3]~K~999" "[PARAMETER3]~K"
"AWK|USL|R|Ford|S|[PARAMETER3]~Q~999" "[PARAMETER3]~K"

相关问题 更多 >