Python 读取文件并解析所需数据
我想读取一个文件并用所需数据解析该文件 文件内容如下所示Python 读取文件并解析所需数据,python,Python,我想读取一个文件并用所需数据解析该文件 文件内容如下所示 ENV: UAT OS_Template: Windows_2012R2,SOE_W2012R2_64_SE_2020Q2:REDHAT7,SOE_REDHAT_7_2019Q2:Windows_2016,SOE_W2016_64_SE_2020Q2 在这里,我想对其进行如下解析 blue_print = ['Windows_2012R2', 'REDHAT7', 'Windows_2016'] image = ['SOE_W2012R
ENV: UAT
OS_Template: Windows_2012R2,SOE_W2012R2_64_SE_2020Q2:REDHAT7,SOE_REDHAT_7_2019Q2:Windows_2016,SOE_W2016_64_SE_2020Q2
在这里,我想对其进行如下解析
blue_print = ['Windows_2012R2', 'REDHAT7', 'Windows_2016']
image = ['SOE_W2012R2_64_SE_2020Q2', 'SOE_REDHAT_7_2019Q2','SOE_W2016_64_SE_2020Q2']
我得到了我想要的答案。下面是代码
with open("blueprint_image.txt" , "r") as f1:
content_list = [line.rstrip('\n') for line in f1]
#Removing Environment
content_list.pop(0)
updated_content_list = "".join(content_list)
print updated_content_list
#converting to string
new_content = updated_content_list.split(":")
#Removing OS template
new_content.pop(0)
print new_content
for i in new_content:
new_list = i.split(",")
print new_list
blueprint.append(new_list[0])
os_template.append(new_list[1])
for items in blueprint:
new_item = items.strip()
blueprint_updated.append(new_item)
for items1 in os_template:
new_item1 = items1.strip()
os_template_updated.append(new_item1)
print blueprint_updated
print os_template_updated
你试图解决这个问题的方法是什么?寻求解决方案和寻求帮助是两件不同的事情。假设第二行是
image:blue\u print
,那么你就不能将Windows\u 2012R2
作为blue\u print
项目,而SOE\u W2016\u 64\u SE\u 2020Q2
是image
项目,如果你在网上分手了,至少试着在谷歌上搜索一些相关的东西,并在询问之前做一些事情。如果您表现出一些意图,我们很乐意提供帮助。很抱歉,我已尝试逐行阅读文件,并先用以下内容拆分该行:。我没有粘贴我的代码bcz,这是错误的。os_tempalte=[]blueprint=[]带有open(“blueprint_image.txt”,“r”)作为f1:content_list=[line.rstrip('\n')用于f1中的行]env_list=content_list.pop(0)content_list=str(content_list)new_content=content_list.split(“:”)new_content.pop(0)用于我在new_content中的内容:new_list=i.split(“,”)blueprint=blueprint.append(新列表[0])os\u template=blueprint.append(新列表[0])
我正在获取属性错误:“非类型”对象没有属性“append”