Python regex to parse text file, get the items in list and count the list
Posted
by
Nemo
on Stack Overflow
See other posts from Stack Overflow
or by Nemo
Published on 2012-06-28T14:29:45Z
Indexed on
2012/06/28
15:16 UTC
Read the original article
Hit count: 492
I have a text file which contains some data.
I m particularly interested in finding the count of the number of items in v_dims
v_dims pattern in my text file looks like this :
v_dims={
"Sales",
"Product Family",
"Sales Organization",
"Region",
"Sales Area",
"Sales office",
"Sales Division",
"Sales Person",
"Sales Channel",
"Sales Order Type",
"Sales Number",
"Sales Person",
"Sales Quantity",
"Sales Amount"
}
So I m thinking of getting all the elements in v_dims and dumping them out in a Python list. Then compute the len(mylist) to get the count of the items.
The challenge is in getting all the elements of v_dims from my text file and putting them in an empty list.
I m particularly interested in items in v_dims in my text file. The text file has data in the form of v_dims pattern i showed in my original post. Some data has nested patterns of v_dims. Thanks.
Here's what I have tried and failed. Any help is appreciated. TIA.
import re
fname = "C:\Users\XXXX\Test.mrk"
with open(fname, "r") as fo:
content_as_string = fo.read()
match = re.findall(r'v_dims={\"(.+?)\"}',content_as_string)
Though I have a big text file, Here's a snippet of what's the structure of my text file
version "1";
// Computer generated object language file
object 'MRKR' "Main" {
Data_Type=2,
HeaderBlock={
Version_String="6.3 (25)"
},
Printer_Info={
Orientation=0,
Page_Width=8.50000000,
Page_Height=11.00000000,
Page_Header="",
Page_Footer="",
Margin_type=0,
Top_Margin=0.50000000,
Left_Margin=0.50000000,
Bottom_Margin=0.50000000,
Right_Margin=0.50000000
},
Marker_Options={
Close_All="TRUE",
Hide_Console="FALSE",
Console_Left="FALSE",
Console_Width=217,
Main_Style="Maximized",
MDI_Rect={
0,
0,
892,
1063
}
},
Dives={
{
Dive="A",
Windows={
{
View_Index=0,
Window_Info={
Window_Rect={
0,
-288,
400,
1008
},
Window_Style="Maximized Front",
Window_Name="Theater [Previous Qtr Diveplan-Dive A]"
},
Dependent_bool="FALSE",
Colset={
Dive_Type="Normal",
Dimension_Name="Theater",
Action_List={
Actions={
{
Action_Type="Select",
select_type=5
},
{
Action_Type="Select",
select_type=0,
Key_Names={
"Theater"
},
Key_Indexes={
{
"AMERICAS"
}
}
},
{
Action_Type="Focus",
Focus_Rows="True"
},
{
Action_Type="Dimensions",
v_dims={
"Theater",
"Product Family",
"Division",
"Region",
"Install at Country Name",
"Connect Home Type",
"Connect In Type",
"SymmConnect Enabled",
"Connect Home Refusal Reason",
"Sales Order Channel Type",
"Maintained By Group",
"PS Flag",
"Avalanche Flag",
"Product Item Family"
},
Xtab_Bool="False",
Xtab_Flip="False"
},
{
Action_Type="Select",
select_type=5
},
{
Action_Type="Select",
select_type=0,
Key_Names={
"Theater",
"Product Family",
"Division",
"Region",
"Install at Country Name",
"Connect Home Type",
"Connect In Type",
"SymmConnect Enabled",
"Connect Home Refusal Reason",
"Sales Order Channel Type",
"Maintained By Group",
"PS Flag",
"Avalanche Flag"
},
Key_Indexes={
{
"AMERICAS",
"ATMOS",
"Latin America CS Division",
"37000 CS Region",
"Mexico",
"",
"",
"",
"",
"DIRECT",
"EMC",
"N",
"0"
}
}
}
}
},
Num_Palette_cols=0,
Num_Palette_rows=0
},
Format={
Window_Type="Tabular",
Tabular={
Num_row_labels=8
}
}
}
}
}
},
Widget_Set={
Widget_Layout="Vertical",
Go_Button=1,
Picklist_Width=0,
Sort_Subset_Dimensions="TRUE",
Order={
}
},
Views={
{
Data_Type=1,
dbname="Previous Qtr Diveplan",
diveline_dbname="Current Qtr Diveplan",
logical_name="Current Qtr Diveplan",
cols={
{
name="Total TSS installs",
column_type="Calc[Total TSS installs]",
output_type="Number",
format_string="."
},
{
name="TSS Valid Connectivity Records",
column_type="Calc[TSS Valid Connectivity Records]",
output_type="Number",
format_string="."
},
{
name="% TSS Connectivity Record",
column_type="Calc[% TSS Connectivity Record]",
output_type="Number"
},
{
name="TSS Not Applicable",
column_type="Calc[TSS Not Applicable]",
output_type="Number",
format_string="."
},
{
name="TSS Customer Refusals",
column_type="Calc[TSS Customer Refusals]",
output_type="Number",
format_string="."
},
{
name="% TSS Refusals",
column_type="Calc[% TSS Refusals]",
output_type="Number"
},
{
name="TSS Eligible for Physical Connectivity",
column_type="Calc[TSS Eligible for Physical Connectivity]",
output_type="Number",
format_string="."
},
{
name="TSS Boxes with Physical Connectivty",
column_type="Calc[TSS Boxes with Physical Connectivty]",
output_type="Number",
format_string="."
},
{
name="% TSS Physical Connectivity",
column_type="Calc[% TSS Physical Connectivity]",
output_type="Number"
}
},
dim_cols={
{
name="Model",
column_type="Dimension[Model]",
output_type="None"
},
{
name="Model",
column_type="Dimension[Model]",
output_type="None"
},
{
name="Connect In Type",
column_type="Dimension[Connect In Type]",
output_type="None"
},
{
name="Connect Home Type",
column_type="Dimension[Connect Home Type]",
output_type="None"
},
{
name="SymmConnect Enabled",
column_type="Dimension[SymmConnect Enabled]",
output_type="None"
},
{
name="Theater",
column_type="Dimension[Theater]",
output_type="None"
},
{
name="Division",
column_type="Dimension[Division]",
output_type="None"
},
{
name="Region",
column_type="Dimension[Region]",
output_type="None"
},
{
name="Sales Order Number",
column_type="Dimension[Sales Order Number]",
output_type="None"
},
{
name="Product Item Family",
column_type="Dimension[Product Item Family]",
output_type="None"
},
{
name="Item Serial Number",
column_type="Dimension[Item Serial Number]",
output_type="None"
},
{
name="Sales Order Deal Number",
column_type="Dimension[Sales Order Deal Number]",
output_type="None"
},
{
name="Item Install Date",
column_type="Dimension[Item Install Date]",
output_type="None"
},
{
name="SYR Last Dial Home Date",
column_type="Dimension[SYR Last Dial Home Date]",
output_type="None"
},
{
name="Maintained By Group",
column_type="Dimension[Maintained By Group]",
output_type="None"
},
{
name="PS Flag",
column_type="Dimension[PS Flag]",
output_type="None"
},
{
name="Connect Home Refusal Reason",
column_type="Dimension[Connect Home Refusal Reason]",
output_type="None",
col_width=177
},
{
name="Cust Name",
column_type="Dimension[Cust Name]",
output_type="None"
},
{
name="Sales Order Channel Type",
column_type="Dimension[Sales Order Channel Type]",
output_type="None"
},
{
name="Sales Order Type",
column_type="Dimension[Sales Order Type]",
output_type="None"
},
{
name="Part Model Key",
column_type="Dimension[Part Model Key]",
output_type="None"
},
{
name="Ship Date",
column_type="Dimension[Ship Date]",
output_type="None"
},
{
name="Model Number",
column_type="Dimension[Model Number]",
output_type="None"
},
{
name="Item Description",
column_type="Dimension[Item Description]",
output_type="None"
},
{
name="Customer Classification",
column_type="Dimension[Customer Classification]",
output_type="None"
},
{
name="CS Customer Name",
column_type="Dimension[CS Customer Name]",
output_type="None"
},
{
name="Install At Customer Number",
column_type="Dimension[Install At Customer Number]",
output_type="None"
},
{
name="Install at Country Name",
column_type="Dimension[Install at Country Name]",
output_type="None"
},
{
name="TLA Serial Number",
column_type="Dimension[TLA Serial Number]",
output_type="None"
},
{
name="Product Version",
column_type="Dimension[Product Version]",
output_type="None"
},
{
name="Avalanche Flag",
column_type="Dimension[Avalanche Flag]",
output_type="None"
},
{
name="Product Family",
column_type="Dimension[Product Family]",
output_type="None"
},
{
name="Project Number",
column_type="Dimension[Project Number]",
output_type="None"
},
{
name="PROJECT_STATUS",
column_type="Dimension[PROJECT_STATUS]",
output_type="None"
}
},
Available_Columns={
"Total TSS installs",
"TSS Valid Connectivity Records",
"% TSS Connectivity Record",
"TSS Not Applicable",
"TSS Customer Refusals",
"% TSS Refusals",
"TSS Eligible for Physical Connectivity",
"TSS Boxes with Physical Connectivty",
"% TSS Physical Connectivity",
"Total Installs",
"All Boxes with Valid Connectivty Record",
"% All Connectivity Record",
"Overall Refusals",
"Overall Refusals %",
"All Eligible for Physical Connectivty",
"Boxes with Physical Connectivity",
"% All with Physical Conectivity"
},
Remaining_columns={
{
name="Total Installs",
column_type="Calc[Total Installs]",
output_type="Number",
format_string="."
},
{
name="All Boxes with Valid Connectivty Record",
column_type="Calc[All Boxes with Valid Connectivty Record]",
output_type="Number",
format_string="."
},
{
name="% All Connectivity Record",
column_type="Calc[% All Connectivity Record]",
output_type="Number"
},
{
name="Overall Refusals",
column_type="Calc[Overall Refusals]",
output_type="Number",
format_string="."
},
{
name="Overall Refusals %",
column_type="Calc[Overall Refusals %]",
output_type="Number"
},
{
name="All Eligible for Physical Connectivty",
column_type="Calc[All Eligible for Physical Connectivty]",
output_type="Number"
},
{
name="Boxes with Physical Connectivity",
column_type="Calc[Boxes with Physical Connectivity]",
output_type="Number"
},
{
name="% All with Physical Conectivity",
column_type="Calc[% All with Physical Conectivity]",
output_type="Number"
}
},
calcs={
{
name="Total TSS installs",
definition="Total[Total TSS installs]",
ts_flag="Not TS Calc"
},
{
name="TSS Valid Connectivity Records",
definition="Total[PS Boxes w/ valid connectivity record (1=yes)]",
ts_flag="Not TS Calc"
},
{
name="% TSS Connectivity Record",
definition="Total[PS Boxes w/ valid connectivity record (1=yes)] /Total[Total TSS installs]",
ts_flag="Not TS Calc"
},
{
name="TSS Not Applicable",
definition="Total[Bozes w/ valid connectivity record (1=yes)]-Total[Boxes Eligible (1=yes)]-Total[TSS Refusals]",
ts_flag="Not TS Calc"
},
{
name="TSS Customer Refusals",
definition="Total[TSS Refusals]",
ts_flag="Not TS Calc"
},
{
name="% TSS Refusals",
definition="Total[TSS Refusals]/Total[PS Boxes w/ valid connectivity record (1=yes)]",
ts_flag="Not TS Calc"
},
{
name="TSS Eligible for Physical Connectivity",
definition="Total[TSS Eligible]-Total[Exception]",
ts_flag="Not TS Calc"
},
{
name="TSS Boxes with Physical Connectivty",
definition="Total[PS Physical Connectivity] - Total[PS Physical Connectivity, SymmConnect Enabled=\"Capable not enabled\"]",
ts_flag="Not TS Calc"
},
{
name="% TSS Physical Connectivity",
definition="Total[Boxes w/ phys conn]/Total[Boxes Eligible (1=yes)]",
ts_flag="Not TS Calc"
},
{
name="Total Installs",
definition="Total[Total Installs]",
ts_flag="Not TS Calc"
},
{
name="All Boxes with Valid Connectivty Record",
definition="Total[Bozes w/ valid connectivity record (1=yes)]",
ts_flag="Not TS Calc"
},
{
name="% All Connectivity Record",
definition="Total[Bozes w/ valid connectivity record (1=yes)]/Total[Total Installs]",
ts_flag="Not TS Calc"
},
{
name="Overall Refusals",
definition="Total[Overall Refusals]",
ts_flag="Not TS Calc"
},
{
name="Overall Refusals %",
definition="Total[Overall Refusals]/Total[Bozes w/ valid connectivity record (1=yes)]",
ts_flag="Not TS Calc"
},
{
name="All Eligible for Physical Connectivty",
definition="Total[Boxes Eligible (1=yes)]-Total[Exception]",
ts_flag="Not TS Calc"
},
{
name="Boxes with Physical Connectivity",
definition="Total[Boxes w/ phys conn]-Total[Boxes w/ phys conn,SymmConnect Enabled=\"Capable not enabled\"]",
ts_flag="Not TS Calc"
},
{
name="% All with Physical Conectivity",
definition="Total[Boxes w/ phys conn]/Total[Boxes Eligible (1=yes)]",
ts_flag="Not TS Calc"
}
},
merge_type="consolidate",
merge_dbs={
{
dbname="connectivityallproducts.mdl",
diveline_dbname="/DI_PSREPORTING/connectivityallproducts.mdl"
}
},
skip_constant_columns="FALSE",
categories={
{
name="Geography",
dimensions={
"Theater",
"Division",
"Region",
"Install at Country Name"
}
},
{
name="Mappings and Flags",
dimensions={
"Connect Home Type",
"Connect In Type",
"SymmConnect Enabled",
"Connect Home Refusal Reason",
"Sales Order Channel Type",
"Maintained By Group",
"Customer Installable",
"PS Flag",
"Top Level Flag",
"Avalanche Flag"
}
},
{
name="Product Information",
dimensions={
"Product Family",
"Product Item Family",
"Product Version",
"Item Description"
}
},
{
name="Sales Order Info",
dimensions={
"Sales Order Deal Number",
"Sales Order Number",
"Sales Order Type"
}
},
{
name="Dates",
dimensions={
"Item Install Date",
"Ship Date",
"SYR Last Dial Home Date"
}
},
{
name="Details",
dimensions={
"Item Serial Number",
"TLA Serial Number",
"Part Model Key",
"Model Number"
}
},
{
name="Customer Infor",
dimensions={
"CS Customer Name",
"Install At Customer Number",
"Customer Classification",
"Cust Name"
}
},
{
name="Other Dimensions",
dimensions={
"Model"
}
}
},
Maintain_Category_Order="FALSE",
popup_info="false"
}
}
};
© Stack Overflow or respective owner