Nobody Reads This

Uther:…and you’ve ground it to dust in a matter of days.

And I have no Illusions Otherwise.
  • Home
  • Images
  • Videos
  • About JamesR

My Python needs a Shaft

James Reynolds | August 28, 2008

You know you’re massively out of practice when it takes 63 lines of Python just to filter an XML file and convert what you get into a CSV file.

I think if I really tried I could get it down to 15-20 well formed lines … I start with something like standard XML trees and finish with:

1,Back_Only,Use_Job_Setting,Tabloid,
2,Different,Use_Job_Setting,Tabloid,
3,Back_Only,Use_Job_Setting,Statement,
4,Different,Use_Job_Setting,Statement,
5,Back_Only,Use_Job_Setting,Executive,
6,Different,Use_Job_Setting,Executive,
7,Back_Only,Use_Job_Setting,8_5_x_13,
8,Different,Use_Job_Setting,8_5_x_13,

The code is thus:

## Fireeye XML Test Plan to CSV converter.
## Scripted by James Reynolds

def main():
print "Filename (*.xml): "
fileName = raw_input()
fileHold = open(fileName, "rb")
fileContent = fileHold.readlines()
fileContent = relist(fileContent)
print "Read and Relisted"
writeTo = open(fileName.replace(".xml",".csv"), 'w')
print "Writing to " + fileName.replace(".xml",".csv")
for i in fileContent:
string = listToCSV(i)
string = remXML(string)
writeTo.write(string)
print "Done."

## Takes XML doc and filters out what we need returning list of tests
def relist(fileContent):
master = []
sub = []
adder = False
for element in fileContent:
if (element.find("Testcase TCNo") >= 0):
adder = True
elif (element.find("") >= 0):
adder = False
sub.append(element)
master.append(sub)
sub = []
if (adder == True):
sub.append(element)
return master

## Takes list and reformats to CSV
def listToCSV(varList):
csvString = ""
for element in varList:
if (csvString == ""):
element = element.replace("\r\n", "")
element = element.strip()
csvString = csvString + element
else:
element = element.replace("\r\n", ",")
element = element.strip()
csvString = csvString + element
csvString = csvString + "\r\n"
return csvString

## Removes XML tags
def remXML(XMLstring):
delete = False
newString = ""
for i in XMLstring:
if (i == "<"):
delete = True
if (i == ">"):
delete = False
if (delete == False and i != ">"):
newString = newString + i
return newString.replace(",,",",")
main()

You can probably see the though process … “I need it to do this. Oh, it won’t? Ok … This. That’s not quite right … What if I had this? Nearly there … And done. Next …”

Clearly not the best way to program … Knowing me I’ll sit down and rewrite it to be a lot better some time soon but … Meh.

Check out todaysbigfail.com, it’s run by another IP here.

Regards,
JamesR.

Wonders if his title is an obvious enough reference.

Comments
No Comments »
Categories
General Junk
Tags
Badly, Converter, Python, Terrible Coding, XML to CSV
Comments rss Comments rss
Trackback Trackback

Tweet

  • I need one more person for http://np.ironhelmet.com/detail?game=1788111 -- Come on you know you want to... it's free :> 2010/03/12

Blogroll

  • Beagle2’s Brain
  • Laputan Machine
  • Lost Caykes
  • Rasberry lOlly’s Blog
  • Resuri’s Blog
  • Twitter (JamesR87)
  • WS@Vlogging
  • Yet Another Blog

Misc

  • Bunny
  • Ctrl+Alt+Delete
  • Cyanide & Happiness
  • Dinosaur Comics
  • Penny Arcade
  • Questionable Content
  • VGCats
  • XboxElite.co.uk
  • XKCD

WoW Blogs

  • Gnomeaggedon
  • MMO Champ
  • WoW.com

Gamercard

Statistics

  • Total Stats
    • 353 Posts
    • 466 Comments

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Cats

Arcs

Calendar

March 2010
M T W T F S S
« Jan    
1234567
891011121314
15161718192021
22232425262728
293031  

Akismet

9,539 spam comments
blocked by
Akismet
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox