Explore Public Snippets
Found 1,593 snippets matching: parsing
public by lbottaro 303311 31 7 0
How to read and parse a txt file in python
import os import sys myfile = open('/some/path/to/file.txt', "r") # EXAMPLE OF FILE FORMAT: # abc 123 234 456 678 # def 122223 2333334 454446 611178 # ghi 144423 211134 44456 61114478 for line in myfile.readlines(): name= '' numList = list() print line try: name = line.split()[0] # it will get 'abc'/'def'/'ghi' numList = line.split()[1:] # it will get all other stuff inline... except: print "Invalid file format" myfile.close() sys.exit(1) if not numList: print("Invalid numList format! name= " + str(name)) myfile.close() sys.exit(1) print 'name='+str(name) print 'numList='+str(numList) myfile.close()
public by lbottaro 4524 5 7 5
How to sort a Javascript array parsing elements using regex pattern
var myCompare = function(a,b) { var matchPattern_A = /^([0-9]+)\/([0-9]+)\/([0-9]+)$/g.exec(a); var matchPattern_B = /^([0-9]+)\/([0-9]+)\/([0-9]+)$/g.exec(b); if(matchPattern_A && matchPattern_B) { //Apply regex sort //I need to sort based on match[1], match[2], match[3] console.log('Apply regex sort'); if (parseInt(matchPattern_A[1], 10) < parseInt(matchPattern_B[1], 10)) return -1; if (parseInt(matchPattern_A[1], 10) > parseInt(matchPattern_B[1], 10)) return 1; else { if (parseInt(matchPattern_A[2], 10) < parseInt(matchPattern_B[2], 10)) return -1; if (parseInt(matchPattern_A[2], 10) > parseInt(matchPattern_B[2], 10)) return 1; else { if (parseInt(matchPattern_A[3], 10) < parseInt(matchPattern_B[3], 10)) return -1; if (parseInt(matchPattern_A[3], 10) > parseInt(matchPattern_B[3], 10)) return 1; return 0; } } } else { //Apply basic lexicographic sort console.log('Apply basic lexicographic sort'); if (a < b) return -1; if (a > b) return 1; return 0; } }; var myArr = [ '5/4/30', '5/10/2', '5/4/3', '1/3/8']; console.log(myArr); myArr.sort(this.myCompare); console.log(myArr); /* Example, having an array as ['5/4/30', '5/10/2', '5/4/3', '1/3/8'] it will be sorted as ['1/3/8', '5/4/3', '5/4/30', '5/10/2'] */
public by lbottaro 3433 2 7 1
Regular Expression In Python - Simple Grouping
import os, re # Assume to have some file names listed # The matching path is: <id>_<name>_<code>.txt # Define regex my_regExp = re.compile(r'(?P<id>^[^_]*)_(?P<name>[^.]*)_(?P<code>[^.]*).txt') for myFile in os.popen('ls -F /some/path').readlines(): id= '' name= '' code= '' try: id= my_regExp.match(myFile).group('id') except: msgErr='Error on id - ' + myFile try: name= my_regExp.match(myFile).group('name') except: msgErr='Error on name - ' + myFile try: code= my_regExp.match(myFile).group('code') except: msgErr='Error on code- ' + myFile
public by lbottaro 2730 2 6 1
Parsing and finding symbolic links in multiple paths
ls -l `find ABC*/code/target-*/TARGET -maxdepth 1 -type l -name "*"` | awk '{print $11}'
public by ejmurray 2456 0 3 -1
cahpter12 for dice into python and XML parsing
<?xml version='1.0' encoding='utf-8'?> <feed> <title>dive into mark</title> <subtitle>currently between addictions</subtitle> <id>tag:diveintomark.org,2001?07?29:/</id> <updated>2009?03?27T21:56:07Z</updated> <link rel='alternate' type='text/html' href='http://diveintomark.org/'/> <link rel='self' type='application/atom+xml' href='http://diveintomark.org/feed/'/> <entry> <author> <name>Mark</name> <uri>http://diveintomark.org/</uri> </author> <title>Dive into history, 2009 edition</title> <link rel='alternate' type='text/html' href='http://diveintomark.org/archives/2009/03/27/dive?into?history?2009?edition'/> <id>tag:diveintomark.org,2009?03?27:/archives/20090327172042</id> <updated>2009?03?27T21:56:07Z</updated> <published>2009?03?27T17:20:42Z</published> <category scheme='http://diveintomark.org' term='diveintopython'/> <category scheme='http://diveintomark.org' term='docbook'/> <category scheme='http://diveintomark.org' term='html'/> <summary type='html'>Putting an entire chapter on one page sounds bloated, but consider this & my longest chapter so far would be 75 printed pages, and it loads in under 5 seconds&hellip; On dialup.</summary> </entry> <entry> <author> <name>Mark</name> <uri>http://diveintomark.org/</uri> </author> <title>Accessibility is a harsh mistress</title> <link> rel='alternate' type='text/html' href='http://diveintomark.org/archives/2009/03/21/accessibility‐is‐a‐harsh‐mistress'</link> <id>tag:diveintomark.org,2009‐03‐21:/archives/20090321200928</id> <updated>2009‐03‐22T01:05:37Z</updated> <published>2009‐03‐21T20:09:28Z</published> <category> scheme='http://diveintomark.org' term='accessibility'</category> <summary> type='html'>The accessibility orthodoxy does not permit people to question the value of features that are rarely useful and rarely used.</summary> </entry> <entry> <author> <name>Mark</name> </author> <title>A gentle introduction to video encoding, part 1: container formats</title> <link> rel='alternate' type='text/html' href='http://diveintomark.org/archives/2008/12/18/give‐part‐1‐container‐formats'</link> <id>tag:diveintomark.org,2008‐12‐18:/archives/20081218155422</id> <updated>2009‐01‐11T19:39:22Z</updated> <published>2008‐12‐18T15:54:22Z</published> <category> scheme='http://diveintomark.org' term='asf'</category> <category> scheme='http://diveintomark.org' term='avi'</category> <category> scheme='http://diveintomark.org' term='encoding'</category> <category> scheme='http://diveintomark.org' term='flv'</category> <category> scheme='http://diveintomark.org' term='GIVE'</category> <category> scheme='http://diveintomark.org' term='mp4'</category> <category> scheme='http://diveintomark.org' term='ogg'</category> <category> scheme='http://diveintomark.org' term='video'</category> <summary> type='html'>These notes will eventually become part of a tech talk on video encoding.</summary> </entry> </feed>
public by lbottaro 4385 1 7 0
How to fetch Internet Resources Using urllib2 in python
import os, urllib2 def getURLResult(): try: intRes = urllib2.urlopen("http://your_URL") except urllib2.HTTPError, e: print "HTTPError Error: " + str(e.code) except urllib2.URLError, e: print "URLError Error: " + str(e.reason) print "ok"
public by mralexgray 2056 0 3 0
Canonical shell argument parsing.
#!/usr/bin/env zsh # Print usage information if there are no arguments on the command line. [ "$#" = "0" ] && echo 'Usage: via-ssh.sh [ -v ] [ -d ] [USER@]SERVER[:PORT]' && exit 1 # Process the command line parameters. while [ "$#" != '0' ] ; do case "$1" in -v) VERBOSE=1 ; shift ;; -d) DNS=1 ; shift ;; *) SSH_PORT=${1##*:} SSH_USER_SERVER=${1%%:*} [ "$SSH_USER_SERVER" = "$SSH_PORT" ] && SSH_PORT=22 shift ;; esac done
#!/bin/bash usage() { echo "" echo "$0 -f <csv|html> -i <IP address> -l" echo "" echo "Example:" echo " $0 -f html -i 172.16.88.83" echo " $0 -f csv -i 172.16.88.83 -l" } #default value FORMAT="csv" IPADDR="" LOAD=false while getopts ":f:i:l" optname do case "$optname" in "f") if [[ ${OPTARG} == "csv" || ${OPTARG} == "html" ]] ; then FORMAT="${OPTARG}" else echo "Output format not supported" usage exit 1 fi ;; "i") IPADDR="${OPTARG}" ;; "l") LOAD=true ;; "?") usage exit 1 ;; ":") echo "No argument value for option $OPTARG" ;; *) echo "Unknown error while processing options" ;; esac done # Mandatory arguments if [[ -z ${FORMAT} ]] || [[ -z ${IPADDR} ]] then usage exit 1 fi echo "format: $FORMAT" echo "ipaddr: $IPADDR" if [[ ${LOAD} == "true" ]] ; then echo "load: true" else echo "load: false" fi
String response = event.getJsonLibraryMetadata(); JsonFactory jf = new JsonFactory(); try { // Log.i("JSON", response); JsonParser jp = jf.createParser(response); while (jp.nextToken() != JsonToken.END_OBJECT) { String token = jp.getCurrentName(); if ("ebooks".equals(token)) { // System.out.println("ebooks :"); EBookDetails mEBookDetails = new EBookDetails(); // the next token will be '[' that means that we have an // array jp.nextToken(); // parse tokens until you find ']' while (jp.nextToken() != JsonToken.END_ARRAY) { // System.out.println(jp.getText()); String currentToken = jp.getCurrentName(); if ("cover_thumbnail".equals(currentToken)) { jp.nextToken(); } if ("scriptures".equals(currentToken)) { jp.nextToken(); } if ("sample".equals(currentToken)) { jp.nextToken(); } if ("created_at".equals(currentToken)) { jp.nextToken(); } if ("updated_at".equals(currentToken)) { jp.nextToken(); } if ("id".equals(currentToken)) { jp.nextToken(); mEBookDetails.id = jp.getIntValue();// ("id"); } if ("archived".equals(currentToken)) { jp.nextToken(); mEBookDetails.isArchived = jp.getValueAsBoolean(); // .getBoolean("archived"); } if ("title".equals(currentToken)) { jp.nextToken(); mEBookDetails.title = jp.getValueAsString();// getString("title"); products.add(mEBookDetails.title); } if ("description".equals(currentToken)) { jp.nextToken(); mEBookDetails.description = jp.getValueAsString(); // .getString("description"); } if ("author".equals(currentToken)) { jp.nextToken(); mEBookDetails.author = jp.getValueAsString();// .getString("author"); } if ("file_name".equals(currentToken)) { jp.nextToken(); mEBookDetails.file_name = jp.getValueAsString();// .getString("file_name"); } if ("genres".equals(currentToken)) { jp.skipChildren(); // while (jp.nextToken() != JsonToken.END_ARRAY) { // // } } if ("authors".equals(currentToken)) { jp.skipChildren(); // while (jp.nextToken() != JsonToken.END_ARRAY) { // // } } if ("topics".equals(currentToken)) { jp.skipChildren(); // while (jp.nextToken() != JsonToken.END_ARRAY) { // } } if ("cover_url".equals(currentToken)) { jp.nextToken(); mEBookDetails.cover_url = jp.getValueAsString();// .getString("cover_url"); // XXX- Stancho - speed optimization String key = "B" + mEBookDetails.id; // XXX- Stancho - speed optimization - // reduce the number of String adding app.book_list.put(key, mEBookDetails); mEBookDetails = new EBookDetails(); } } } } jp.close();
public by msdn 147927 0 7 0
Parse: Parses the hard disk SettingData embedded instance returned from the server and creates a new VirtualHardDiskSettingData with that information.
/// <summary> /// Parses the hard disk SettingData embedded instance returned from the server and /// creates a new VirtualHardDiskSettingData with that information. /// </summary> /// <param name="embeddedInstance">The disk SettingData embedded instance.</param> /// <returns>A <see cref="VirtualHardDiskSettingData"/> object with the data contained in /// the embedded instance.</returns> /// <exception cref="ArgumentNullException">If either param is null.</exception> /// <exception cref="FormatException">If there was a problem parsing the embedded instance. /// </exception> public static VirtualHardDiskSettingData Parse( string embeddedInstance) { VirtualHardDiskType type = VirtualHardDiskType.Unknown; VirtualHardDiskFormat format = VirtualHardDiskFormat.Unknown; string path = string.Empty; string parentPath = string.Empty; Int64 maxInternalSize = 0; Int32 blockSize = 0; Int32 logicalSectorSize = 0; Int32 physicalSectorSize = 0; XmlDocument doc = new XmlDocument(); doc.LoadXml(embeddedInstance); XmlNodeList nodelist = doc.SelectNodes(@"/INSTANCE/@CLASSNAME"); if (nodelist.Count != 1) { throw new FormatException(); } if (nodelist[0].Value != "Msvm_VirtualHardDiskSettingData") { throw new FormatException(); } // Disk type nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'Type']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } int itype = int.Parse(nodelist[0].Value, NumberStyles.None, CultureInfo.InvariantCulture); type = (VirtualHardDiskType)itype; if (type != VirtualHardDiskType.Differencing && type != VirtualHardDiskType.DynamicallyExpanding && type != VirtualHardDiskType.FixedSize) { // The type integer returned is of an unrecognized type. throw new FormatException(); } // Disk format nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'Format']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } int iformat = int.Parse(nodelist[0].Value, NumberStyles.None, CultureInfo.InvariantCulture); format = (VirtualHardDiskFormat)iformat; if (format != VirtualHardDiskFormat.Vhd && format != VirtualHardDiskFormat.Vhdx) { //The format integer returned is of an unrecognized type. throw new FormatException(); } // Path nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'Path']/VALUE/child::text()"); if (nodelist.Count != 1) { // There can not be multiple parents. throw new FormatException(); } path = nodelist[0].Value; // ParentPath nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'ParentPath']/VALUE/child::text()"); // A nodeList.Count == 0 is okay and indicates that there is no parent. if (nodelist.Count == 1) { parentPath = nodelist[0].Value; } else if (nodelist.Count != 0) { // There can not be multiple parents. throw new FormatException(); } if (type == VirtualHardDiskType.Differencing && string.IsNullOrEmpty(parentPath)) { // Parent path must be set if this is a differencing disk. throw new FormatException(); } // MaxInternalSize nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'MaxInternalSize']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } maxInternalSize = Int64.Parse(nodelist[0].Value, CultureInfo.InvariantCulture); // BlockSize nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'BlockSize']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } blockSize = Int32.Parse(nodelist[0].Value, CultureInfo.InvariantCulture); // LogicalSectorSize nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'LogicalSectorSize']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } logicalSectorSize = Int32.Parse(nodelist[0].Value, CultureInfo.InvariantCulture); // PhysicalSectorSize nodelist = doc.SelectNodes(@"//PROPERTY[@NAME = 'PhysicalSectorSize']/VALUE/child::text()"); if (nodelist.Count != 1) { throw new FormatException(); } physicalSectorSize = Int32.Parse(nodelist[0].Value, CultureInfo.InvariantCulture); return new VirtualHardDiskSettingData(type, format, path, parentPath, maxInternalSize, blockSize, logicalSectorSize, physicalSectorSize); }