|
|
Up |
|
|
  |
Author: TYRTYR Date: Jun 20, 2008 04:01
OK, this ought to be simple. I'm parsing a large text file (originally
a database dump) in order to process the contents back into a SQLite3
database. The data looks like this:
'AAA','PF',-17.416666666667,-145.5,'Anaa, French Polynesia','Pacific/
Tahiti','Anaa';'AAB','AU',-26.75,141,'Arrabury, Queensland,
Australia','?','?';'AAC','EG',31.133333333333,33.8,'Al Arish,
Egypt','Africa/Cairo','El Arish International';'AAE','DZ',
36.833333333333,8,'Annaba','Africa/Algiers','Rabah Bitat';
which goes on for another 308 lines. As keen and agile minds will no
doubt spot, the rows are separated by a ; so it should be simple to
parse it using a regex. So, I establish a db connection and cursor,
create the table, and open the source file.
Then we do this:
f = file.readlines()
biglist = re.split(';', f)
and then iterate over the output from re.split(), inserting each set
of values into the db, and finally close the file and commit
transactions. But instead, I get this error:
|
| Show full article (1.33Kb) |
|
| | 7 Comments |
|
  |
Author: Peter OttenPeter Otten Date: Jun 20, 2008 04:12
TYR wrote:
> OK, this ought to be simple. I'm parsing a large text file (originally
> a database dump) in order to process the contents back into a SQLite3
> database. The data looks like this:
>
> 'AAA','PF',-17...
|
| Show full article (1.76Kb) |
|
| | no comments |
|
  |
Author: MelMel Date: Jun 20, 2008 04:15
TYR wrote:
> OK, this ought to be simple. I'm parsing a large text file (originally
> a database dump) in order to process the contents back into a SQLite3
> database. The data looks like this:
>
> 'AAA','PF',-17...
|
| Show full article (1.40Kb) |
| no comments |
|
  |
Author: John MachinJohn Machin Date: Jun 20, 2008 05:06
On Jun 20, 9:01 pm, TYR gmail.com> wrote:
> OK, this ought to be simple. I'm parsing a large text file (originally
> a database dump) in order to process the contents back into a SQLite3
> database. The data looks like this:
>
> 'AAA','PF',-17.416666666667,-145.5,'Anaa, French Polynesia','Pacific/
> Tahiti','Anaa';'AAB','AU',-26.75,141,'Arrabury, Queensland,
> Australia','?','?';'AAC','EG',31.133333333333,33.8,'Al Arish,
> Egypt','Africa/Cairo','El Arish International';'AAE','DZ',
> 36.833333333333,8,'Annaba','Africa/Algiers','Rabah Bitat';
>
> which goes on for another 308 lines.
308 lines or 308 rows? Another way of asking the same question: do you
have line terminators like \n or \r\n or \r in your file? If so, you
will need to do something like this:
rows = open('myfile', 'rb').read().replace('\r\n', '').split(';')
|
| Show full article (2.29Kb) |
| no comments |
|
  |
Author: TYRTYR Date: Jun 20, 2008 05:33
>How do you propose to parse that string into a "set of values"? Can
>you rely there being data commas only in the 5th field, or do you need
>a general solution? What if (as Peter remarked) there is a ';' in the
>data? What if there's a "'" in the data (think O'Hare)?
My plan was to be pointlessly sarcastic.
|
| |
| no comments |
|
  |
Author: John MachinJohn Machin Date: Jun 20, 2008 05:43
On Jun 20, 10:33 pm, TYR gmail.com> wrote:
>>How do you propose to parse that string into a "set of values"? Can
>>you rely there being data commas only in the 5th field, or do you need
>>a general solution? What if (as Peter remarked) there is a ';' in the
>>data? What if there's a "'" in the data (think O'Hare)?
>
> My plan was to be pointlessly sarcastic.
You misunderstand. My questions covered several of the problems often
encountered by newbies. I am offering help. If my suspicion that you
would need further help was incorrect, please forgive me.
|
| |
| no comments |
|
  |
Author: Paul McGuirePaul McGuire Date: Jun 20, 2008 07:35
On Jun 20, 6:01Â am, TYR gmail.com> wrote:
> OK, this ought to be simple. I'm parsing a large text file (originally
> a database dump) in order to process the contents back into a SQLite3
> database. The data looks like this:
>
> 'AAA','PF',-17.416666666667,-145.5,'Anaa, French Polynesia','Pacific/
> Tahiti','Anaa';'AAB','AU',-26.75,141,'Arrabury, Queensland,
> Australia','?','?';'AAC','EG',31.133333333333,33.8,'Al Arish,
> Egypt','Africa/Cairo','El Arish International';'AAE','DZ',
> 36.833333333333,8,'Annaba','Africa/Algiers','Rabah Bitat';
>
> which goes on for another 308 lines. As keen and agile minds will no
> doubt spot, the rows are separated by a ; so it should be simple to
> parse it using a regex. So, I establish a db connection and cursor,
> create the table, and open...
|
| Show full article (3.08Kb) |
| no comments |
|
  |
|
|
  |
Author: TYRTYR Date: Jun 21, 2008 10:09
On Jun 20, 3:35Â pm, Paul McGuire austin.rr.com> wrote:
> On Jun 20, 6:01Â am, TYR gmail.com> wrote:
Thank you very much. This pyparsing module looks damned useful.
|
| |
| no comments |
|
|