如何根据数据框列的唯一值创建文本文件?
How to create a text file based on the unique values of a dataframe column?
我有一个 excel table,其中有 2 个列('NE' 和 'Interface'),我想做的是:编辑一个 .txt-每个接口值的文件模板(我已经有了,我在下面显示)。然后连接属于同一组'NE'.
的txt文件
这是我的 excel:
这是我的 txt 文件模板,我想用 excel:
的接口值更改“接口”
conf t
**$interface**
no service-policy input QOS-IN_ACCESS
end
conf t
no policy-map QOS-IN_ACCESS
policy-map QOS-IN_ACCESS
class DSCP_VOIX_SIG
set mpls experimental imposition 5
set qos-group 5
end
conf t
**$interface**
service-policy input QOS-IN_ACCESS
end
这是我的代码:(我已经连接了文件,我需要做的是将它们放在一组 NE 中)
from string import Template
import pandas as pd
df3 = pd.read_excel(r"C:\Users\audit_policymap.xlsx")
with open(r"C:\Users\audit_policymap.txt") as fp:
template = Template(fp.read())
content2 = ''
content3 = ''
for i in range(len(df3)):
file_name = df.loc[i, "NE"] + '_output.txt'
with open(file_name, 'w') as fp:
content = template.substitute(interface=df.loc[i, "Interface"])
if df.loc[i, "NE"] == df.loc[i+1, "NE"]:
content2 = str(content2)+'\n'+str(content)+'\n'
content3 = str(content2)+'\n'
fp.write(content2)
else:
content2 = ''
content3 = str(content3)+'\n'+str(content)+'\n'
fp.write(content3)
总结:我想根据相应的 'NE'
使用所有界面编辑每个 'NE' 的一个 txt 文件
- 在
NE
列上使用 pandas.DataFrame.groupby。
- 此 returns 一个
DataFrameGroupBy
对象,其中 i
是来自 NE
的唯一 groupby 值,g
是关联组。
- for 循环将遍历
NE
中的每个唯一值
- 使用 f 字符串指定唯一的文件名(例如
f'{i}_output.txt'
)
'250002-PEFTTS-2_output.txt'
- 因为
g
中的所有值仅属于 NE
中的唯一值之一,因此无需像您所做的那样检查 NE
是否匹配每一行在问题中。
[str(template.substitute(interface=row)) for row in g.Interface]
是一个列表理解,对于 g.Interface
中的每一行,将 str(template.substitute(interface=row))
添加到列表中。
'\n'.join()
将列表中的每个项目连接为由换行符分隔的字符串。
for i, g in df.groupby('NE'): # iterate through unique values in NE
file_name = f'{i}_output.txt' # create the empty content string
with open(file_name, 'w') as fp: # open the file
content = '\n'.join([str(template.substitute(interface=row)) for row in g.Interface])
fp.write(content) # write content to file
我有一个 excel table,其中有 2 个列('NE' 和 'Interface'),我想做的是:编辑一个 .txt-每个接口值的文件模板(我已经有了,我在下面显示)。然后连接属于同一组'NE'.
的txt文件这是我的 excel:
这是我的 txt 文件模板,我想用 excel:
的接口值更改“接口”conf t
**$interface**
no service-policy input QOS-IN_ACCESS
end
conf t
no policy-map QOS-IN_ACCESS
policy-map QOS-IN_ACCESS
class DSCP_VOIX_SIG
set mpls experimental imposition 5
set qos-group 5
end
conf t
**$interface**
service-policy input QOS-IN_ACCESS
end
这是我的代码:(我已经连接了文件,我需要做的是将它们放在一组 NE 中)
from string import Template
import pandas as pd
df3 = pd.read_excel(r"C:\Users\audit_policymap.xlsx")
with open(r"C:\Users\audit_policymap.txt") as fp:
template = Template(fp.read())
content2 = ''
content3 = ''
for i in range(len(df3)):
file_name = df.loc[i, "NE"] + '_output.txt'
with open(file_name, 'w') as fp:
content = template.substitute(interface=df.loc[i, "Interface"])
if df.loc[i, "NE"] == df.loc[i+1, "NE"]:
content2 = str(content2)+'\n'+str(content)+'\n'
content3 = str(content2)+'\n'
fp.write(content2)
else:
content2 = ''
content3 = str(content3)+'\n'+str(content)+'\n'
fp.write(content3)
总结:我想根据相应的 'NE'
使用所有界面编辑每个 'NE' 的一个 txt 文件- 在
NE
列上使用 pandas.DataFrame.groupby。- 此 returns 一个
DataFrameGroupBy
对象,其中i
是来自NE
的唯一 groupby 值,g
是关联组。 - for 循环将遍历
NE
中的每个唯一值
- 此 returns 一个
- 使用 f 字符串指定唯一的文件名(例如
f'{i}_output.txt'
)'250002-PEFTTS-2_output.txt'
- 因为
g
中的所有值仅属于NE
中的唯一值之一,因此无需像您所做的那样检查NE
是否匹配每一行在问题中。 [str(template.substitute(interface=row)) for row in g.Interface]
是一个列表理解,对于g.Interface
中的每一行,将str(template.substitute(interface=row))
添加到列表中。'\n'.join()
将列表中的每个项目连接为由换行符分隔的字符串。
for i, g in df.groupby('NE'): # iterate through unique values in NE
file_name = f'{i}_output.txt' # create the empty content string
with open(file_name, 'w') as fp: # open the file
content = '\n'.join([str(template.substitute(interface=row)) for row in g.Interface])
fp.write(content) # write content to file