Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 28th, 2012, 11:42 AM
magicjack89 magicjack89 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 1 magicjack89 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 20 m 50 sec
Reputation Power: 0
Python to parse text file and get the count of items

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

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.

Here's what my text file looks like

Code:
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"
}
	}
};

Here's what I have tried and failed. Any help is appreciated. TIA.

Code:
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)

Reply With Quote
  #2  
Old June 29th, 2012, 03:50 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
Please show an example of nested v_dims and the output you'd like from it. Thank you.
Also, is there a singular case of v_dims without braces as in
v_dims = "theater";

Finally, flex and bison are better tools for this project than is python. flex and bison are specifically designed to parse languages.
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #3  
Old June 30th, 2012, 08:14 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,389 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 14 h 22 m 25 sec
Reputation Power: 383
flex and bison are available under a gnu software license on many platforms. Here's an example use of program to follow. ./vdims is the name of the program, vdims.txt is your sample input file.
Code:
$ ./vdims<vdims.txt
v_dims: 14 items
$

These are the build instructions on unix:
Code:
$ bison -d vdims.y  # expect 2 reduce/reduce conflicts
$ flex --outfile=vdims.lex.c vdims.lex
$ gcc -o vdims -Wall vdims.tab.c vdims.lex.c -ly
The code compiles with warnings. Since I've shown more interest than have you, I'm not going to invest more effort right away.

Here's the bison source file, named vdims.y
Code:
%{
#define YYSTYPE char*

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define STREQ(A,B) (!strcmp((A),(B)))

int
  yylex(void),
  vdims = 0;

void yyerror (char const*s) {
  fprintf(stderr,"\nError: %s\n",s);
  exit(EXIT_FAILURE);
}

%}

%left VERSION OBJECT WORD BURP STRING NUMBER COMMA SEMICOLON BLOCK ENDBLOCK ASSIGN

%%

source: version object {
      return EXIT_SUCCESS;
    }
  ;

version: VERSION STRING SEMICOLON
  ;

object: OBJECT BURP STRING block SEMICOLON
  ;

block: BLOCK statements ENDBLOCK
  ;

statements: statement | statements COMMA statement
  ;

statement: assignment
  ;

assignment: WORD ASSIGN {
      if (STREQ("v_dims",$1))
	vdims = 1;
      free($1);
    } rhs {
      if (vdims)
        printf("v_dims: %d items\n",vdims-2), vdims = 0;
    }
  ;

rhs: block
  | expression
  ;

expression: STRING { vdims += !!vdims; }
  | NUMBER { vdims += !!vdims; }
  | list { vdims += !!vdims; }
  | block { vdims += !!vdims; }
  ;

list: BLOCK two_kinds_of_lists ENDBLOCK
  ;

two_kinds_of_lists: empty
  | expressions
  ;

empty: /* empty ok */
  ;

expressions: expression
  | expressions COMMA expression
  ;
And finally, this is the lexical analysis file, named vdims.lex
Code:
%{
#include<string.h>
#include"vdims.tab.h"

int yywrap(void) {
  return 1;
}
%}

STRING \"([^"]|(\\\"))*\"
INTEGER [0-9]+
FLOAT {INTEGER}[.]{INTEGER}
NUMBER -?({FLOAT}|{INTEGER})
COMMA ,
SEMICOLON ;
COMMENT "//".*
BLOCK "{"
ENDBLOCK "}"
ASSIGN =
LETTER [a-zA-Z]
WORD {LETTER}({LETTER}|[_0-9])*
BURP '{WORD}'
VERSION version
OBJECT object

%%

{COMMENT} ;			/* ignore */
{VERSION} return VERSION;
{OBJECT} return OBJECT;
{WORD} return yylval = strdup(yytext), WORD;
{BURP} return BURP;
{STRING} return STRING;
{NUMBER} return NUMBER;
{COMMA} return COMMA;
{SEMICOLON} return SEMICOLON;
{BLOCK} return BLOCK;
{ENDBLOCK} return ENDBLOCK;
{ASSIGN} return ASSIGN;

.|\n ;

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Python to parse text file and get the count of items

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap