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
asked Nov 1, 2012 at 20:34
Tristan Forward
3,5747 gold badges37 silver badges43 bronze badges
-
What does your program look like? What trouble are you having?Gerrat– Gerrat2012年11月01日 20:38:03 +00:00Commented Nov 1, 2012 at 20:38
-
Updated in response to commentsTristan Forward– Tristan Forward2012年11月01日 20:43:07 +00:00Commented Nov 1, 2012 at 20:43
3 Answers 3
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('|')])
answered Nov 1, 2012 at 20:38
Jon Clements
143k34 gold badges254 silver badges288 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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.
answered Nov 1, 2012 at 20:48
anijhaw
9,4927 gold badges38 silver badges37 bronze badges
Comments
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
lang-py