1

I have the following string

string = "OGC Number | LT No | Job /n 9625878 | EPP3234 | 1206545/n" and continues on  

I am trying to write it to a .CSV file where it will look like this:

OGC Number | LT No | Job   
------------------------------
9625878   | EPP3234  | 1206545
9708562   | PGP43221 | 1105482
9887954   | BCP5466  | 1025454
  • where each newline in the string is a new row
  • where each "|" in the sting is a new column

I am having trouble getting the formatting.

I think I need to use:

string.split('/n')
string.split('|')

Thanks.

Windows 7, Python 2.6

2
  • What does your program look like? What trouble are you having? Commented Nov 1, 2012 at 20:38
  • Updated in response to comments Commented Nov 1, 2012 at 20:43

3 Answers 3

1

Untested:

text="""
OGC Number | LT No | Job   
------------------------------
9625878   | EPP3234  | 1206545
9708562   | PGP43221 | 1105482
9887954   | BCP5466  | 1025454"""

import csv
lines = text.splitlines()
with open('outputfile.csv', 'wb') as fout:
    csvout = csv.writer(fout)
    csvout.writerow(lines[0]) # header
    for row in lines[2:]: # content
        csvout.writerow([col.strip() for col in row.split('|')])
Sign up to request clarification or add additional context in comments.

Comments

0

If you are interested in using a third party module. Prettytable is very useful and has a nice set of features to deal with and print tabular data.

Comments

0

EDIT: Oops, I missunderstood your question!

The code below will use two regular expressions to do the modifications.

import re

str="""OGC Number | LT No | Job   
------------------------------
9625878   | EPP3234  | 1206545
9708562   | PGP43221 | 1105482
9887954   | BCP5466  | 1025454
"""
# just setup above 

# remove all lines with at least 4 dashes
str=re.sub( r'----+\n', '', str )

# replace all pipe symbols with their 
# surrounding spaces by single semicolons
str=re.sub( r' +\| +', ';', str )

print str

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.