473,372 Members | 1,175 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

parsing a dbIII file

Hello everybody, I'm new to python (...I work with cobol...)

I have to parse a file (that is a dbIII file) whose stucture look like
this:
|string|, |string|, |string that may contain commas inside|, 1, 2, 3, |
other string|

Is there anything in python that parses this stuff?
thanks a lot
korovev

Aug 7 '07 #1
6 1484
ko*******@gmail.com wrote:
Hello everybody, I'm new to python (...I work with cobol...)

I have to parse a file (that is a dbIII file) whose stucture look like
this:
|string|, |string|, |string that may contain commas inside|, 1, 2, 3, |
other string|

Is there anything in python that parses this stuff?
thanks a lot
korovev
That's not a standard dBaseIII data file though, correct? It looks more
like something that was produced *from* a dBaseIII file.

If the format is similar to Excel's CSV format then the csv module from
Python's standard library may well be what you want. Otherwise there are
parsers at all levels - one called PyParsing is quite popular, and I am
sure other readers will have their own suggestions.

I am not sure whether the pipe bars actually appear in your data file,
so it is difficult to know quite exactly what to suggest, but I would
play with the file in an interactive interpreter session first to see
whether csv can do the job.

Good luck with your escape from COBOL!

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Aug 7 '07 #2
On 7 Ago, 09:21, Steve Holden <st...@holdenweb.comwrote:
That's not a standard dBaseIII data file though, correct? It looks more
like something that was produced *from* a dBaseIII file.
yeap... unfortunately it is not...
Good luck with your escape from COBOL!
i'm not escaping by now... Actually I'd like to use cobol for the rest
of my life (as a programmer) ;-)
But thanx anyway!

korovev

Aug 7 '07 #3
ko*******@gmail.com wrote:
Hello everybody, I'm new to python (...I work with cobol...)

I have to parse a file (that is a dbIII file) whose stucture look like
this:
|string|, |string|, |string that may contain commas inside|, 1, 2, 3, |
other string|
There are a number of relatively simple options that come to mind, including
regular expressions:

##### BEGIN CODE #####

import re

#
# dbIII.txt:
# |string|, |string|, |string|, |string|, |,1,2,3,4|, |other string|
#
handle = open('dbIII.txt')
for line in handle.xreadlines():
for match in re.finditer(r'\|\s*([^|]+)\s*\|,*', line):
for each in match.groups():
print each

handle.close()

##### END CODE #####
Without knowing what you need to do with the data, it's hard to suggest a better
method for parsing it. The above should work, provided that the data is always
in the format | data | with no pipe symbols in between the ones used as separators.

HTH,

-Jay
Aug 7 '07 #4
On Aug 7, 2:21 am, Steve Holden <st...@holdenweb.comwrote:
korove...@gmail.com wrote:
Hello everybody, I'm new to python (...I work with cobol...)
I have to parse a file (that is a dbIII file) whose stucture look like
this:
|string|, |string|, |string that may contain commas inside|, 1, 2, 3, |
other string|
As Steve mentioned pyparsing, here is a pyparsing version for cracking
your data:

from pyparsing import *

data = "|string|, |string|, |string that may contain commas inside|,
1, 2, 3, |other string|"

integer = Word(nums)
# change unquoteResults to True to omit '|' chars from results
string = QuotedString("|", unquoteResults=False)
itemList = delimitedList( integer | string )

# parse the data and print out the results as a simple list
print itemList.parseString(data).asList()

# add a parse action to convert integer strings to actual integers
integer.setParseAction(lambda t:int(t[0]))

# reparse the data and now get converted integers in results
print itemList.parseString(data).asList()

Prints:

['|string|', '|string|', '|string that may contain commas inside|',
'1', '2', '3', '|other string|']
['|string|', '|string|', '|string that may contain commas inside|', 1,
2, 3, '|other string|']

-- Paul

Aug 7 '07 #5
On 8/7/07, ko*******@gmail.com <ko*******@gmail.comwrote:
I have to parse a file (that is a dbIII file) whose stucture look like
this:
|string|, |string|, |string that may contain commas inside|, 1, 2, 3, |
other string|
The CSV module is probably the easiest way to go:
>>data = "|string|, |string|, |string that may contain commas
inside|, 1, 2, 3, |other string|"
>>import csv
reader = csv.reader([data], quotechar="|", skipinitialspace=True)
for row in reader:
print row

['string', 'string', 'string that may contain commas inside', '1',
'2', '3', 'other string']

--
Jerry
Aug 7 '07 #6
On 7 Ago, 17:47, "Jerry Hill" <malaclyp...@gmail.comwrote:
On 8/7/07, korove...@gmail.com <korove...@gmail.comwrote:
I have to parse a file (that is a dbIII file) whose stucture look like
this:
|string|, |string|, |string that may contain commas inside|, 1, 2, 3, |
other string|

The CSV module is probably the easiest way to go:
>data = "|string|, |string|, |string that may contain commas

inside|, 1, 2, 3, |other string|">>import csv
>reader = csv.reader([data], quotechar="|", skipinitialspace=True)
for row in reader:

print row

['string', 'string', 'string that may contain commas inside', '1',
'2', '3', 'other string']

--
Jerry

you all were right, I had to mention that I must put the datas in
mysql... So actually the best way to do it is with csv.reader: i tried
it and it works out!

thanx very much!

Aug 8 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Willem Ligtenberg | last post by:
I decided to use SAX to parse my xml file. But the parser crashes on: File "/usr/lib/python2.3/site-packages/_xmlplus/sax/handler.py", line 38, in fatalError raise exception...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
1
by: G.Esmeijer | last post by:
Friends, I have some data that I want to access (und update) and which is stored in DBase III files. The application that is working with this data is wrtten in Clipper (Those were the days...
1
by: Christoph Bisping | last post by:
Hello! Maybe someone is able to give me a little hint on this: I've written a vb.net app which is mainly an interpreter for specialized CAD/CAM files. These files mainly contain simple movement...
4
by: Rick Walsh | last post by:
I have an HTML table in the following format: <table> <tr><td>Header 1</td><td>Header 2</td></tr> <tr><td>1</td><td>2</td></tr> <tr><td>3</td><td>4</td></tr> <tr><td>5</td><td>6</td></tr>...
3
by: toton | last post by:
Hi, I have some ascii files, which are having some formatted text. I want to read some section only from the total file. For that what I am doing is indexing the sections (denoted by .START in...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
13
by: charliefortune | last post by:
I am fetching some product feeds with PHP like this $merch = substr($key,1); $feed = file_get_contents($_POST); $fp = fopen("./feeds/feed".$merch.".txt","w+"); fwrite ($fp,$feed); fclose...
2
by: Felipe De Bene | last post by:
I'm having problems parsing an HTML file with the following syntax : <TABLE cellspacing=0 cellpadding=0 ALIGN=CENTER BORDER=1 width='100%'> <TH BGCOLOR='#c0c0c0' Width='3%'>User ID</TH> <TH...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.