Converting PGN to CSV file

Discussion of chess software programming and technical issues.

Moderator: Ras

Dann Corbit
Posts: 12791
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Converting PGN to CSV file

Post by Dann Corbit »

Jon12345 wrote:The tool I want to use is not Access, although I mentioned Access as I could manipulate it there and export it as CSV.

I have found the PGN format very tricky to manipulate. Wondering if you have to use RegEx to do it.
Regex is the wrong solution. You need a parser.

You could do it with Regex, but you would need a lot of rules.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Converting PGN to CSV file

Post by Don »

Dann Corbit wrote:
Jon12345 wrote:The tool I want to use is not Access, although I mentioned Access as I could manipulate it there and export it as CSV.

I have found the PGN format very tricky to manipulate. Wondering if you have to use RegEx to do it.
Regex is the wrong solution. You need a parser.

You could do it with Regex, but you would need a lot of rules.
So the question for me is why he wants it in CSV. He wants to go from CSV to something else, but it's not clear what that something else is.

From what I can see so far he does not want it in a spreadsheet or in Access, unless that enables him to convert to that other mystery format.
Jon12345
Posts: 80
Joined: Tue May 11, 2010 6:18 pm

Re: Converting PGN to CSV file

Post by Jon12345 »

There is nothing secret about it. We have a proprietary tool that takes our CSV data and squirts it into a website. The problem we are facing is that in this case, PGN format is more difficult to convert - for me anyway!
Jon
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Converting PGN to CSV file

Post by Don »

Jon12345 wrote:There is nothing secret about it. We have a proprietary tool that takes our CSV data and squirts it into a website. The problem we are facing is that in this case, PGN format is more difficult to convert - for me anyway!
Ok, so the issue is what do you want the CSV file to look like. Please post an example of a CSV with 2 games scores, exactly like you need it.

Here is what 1 line of a typical CSV record looks like:

"Dailey","Don","Software Engineer",54,"540-366-3110"

A full record is on a single line and comma's between fields in the record. numbers do not have to be quoted although CSV is not very "standardized" about some of the details.

A pgn game has a lot of linefeeds in it, and I'm not sure how you represent records with several lines in CSV. In C you can use escape characters but I have no idea how it's handled in CSV - although it could probably be researched on the web.

So it's rather unclear exactly what it is you need. Also, pgn records have quotes inside of them that also need to be escaped properly.
Jon12345
Posts: 80
Joined: Tue May 11, 2010 6:18 pm

Re: Converting PGN to CSV file

Post by Jon12345 »

I think for the sake of simplicity (if such a thing exists!) that I could take just the moves.

1. e4 c5 2. f4 e6 3. Nf3 Nc6 4. c3 d5 5. e5 f5 6. d4 Nh6 0-1
1. e4 c5 2. f4 e6 3. Nf3 Nc6 4. c3 d5 5. e5 f5 6. d4 Nh6 7. a3 Nf7 8. Be3 0-1

So there is just 1 field of data. The thing that confuses me is that there are end of line hidden characters in the text file. So when you have a CSV file, what happens there?

Anyway, the above is worth it for me to test to see if it works.
Jon
Dann Corbit
Posts: 12791
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Converting PGN to CSV file

Post by Dann Corbit »

Jon12345 wrote:I think for the sake of simplicity (if such a thing exists!) that I could take just the moves.

1. e4 c5 2. f4 e6 3. Nf3 Nc6 4. c3 d5 5. e5 f5 6. d4 Nh6 0-1
1. e4 c5 2. f4 e6 3. Nf3 Nc6 4. c3 d5 5. e5 f5 6. d4 Nh6 7. a3 Nf7 8. Be3 0-1

So there is just 1 field of data. The thing that confuses me is that there are end of line hidden characters in the text file. So when you have a CSV file, what happens there?

Anyway, the above is worth it for me to test to see if it works.
Try PGN Extract by Barnes and set the line width to 10000 characters. That will read a PGN file (and clean it if you like) and emit all the moves as a single line. For example:

Code: Select all

[Event "Leningrad Championships"]
[Site "Leningrad"]
[Date "1972.??.??"]
[Round "?"]
[White "Aron G Reshko"]
[Black "Oleg Kaminsky"]
[Result "1-0"]
[WhiteElo "?"]
[BlackElo "?"]
[ECO "A29"]
[EventDate "?"]
[PlyCount "141"]

1. c4 Nf6 2. Nc3 e5 3. Nf3 Nc6 4. g3 d5 5. cxd5 Nxd5 6. Bg2 Nb6 7. O-O Be7 8. a4 a5 9. b3 O-O 10. Bb2 Be6 11. Nb5 Bf6 12. Rc1 Qc8 13. d4 Rd8 14. e4 exd4 15. e5 Be7 16. Nfxd4 Nxd4 17. Bxd4 c6 18. Qe2 Nd5 19. Bb2 Nc7 20. Nd4 Bd5 21. f4 Bxg2 22. Kxg2 Bb4 23. f5 c5 24. e6 cxd4 25. Qe5 Bc3 26. Bxc3 f6 27. Qe2 dxc3 28. Rxc3 Rd6 29. Rd1 Rc6 30. Rcd3 Ne8 31. Qh5 Rc7 32. Rd7 Rxd7 33. exd7 Qd8 34. Qf3 Nc7 35. Qxb7 Kf8 36. Rc1 Qxd7 37. Rxc7 Qd2+ 38. Kf3 Qd1+ 39. Kf4 Qd4+ 40. Qe4 Qd6+ 41. Kg4 Re8 42. Qc4 Re4+ 43. Qxe4 Qxc7 44. b4 axb4 45. Qxb4+ Kg8 46. a5 h6 47. a6 Kh7 48. Qd4 Qa5 49. a7 Qa6 50. Kh4 Qa2 51. h3 Qa5 52. Qd7 Qb4+ 53. Kh5 Qa3 54. Kg4 Qe3 55. h4 Qe4+ 56. Kh5 Qf3+ 57. g4 Qe4 58. Qf7 Qc6 59. Qe7 Qd5 60. Qe8 Qb7 61. a8=B Qb3 62. Qd7 Qg8 63. Bd5 Qf8 64. Bf7 Kh8 65. Qe8 Qxe8 66. Bxe8 Kh7 67. Bf7 Kh8 68. Kg6 h5 69. Kxh5 Kh7 70. Be8 Kg8 71. Kg6 1-0
[/code]
Jon12345
Posts: 80
Joined: Tue May 11, 2010 6:18 pm

Re: Converting PGN to CSV file

Post by Jon12345 »

I've just install that program. it looks very tricky to run. Is it command line or something? Do I run it in a DOS window?
Jon
Dann Corbit
Posts: 12791
Joined: Wed Mar 08, 2006 8:57 pm
Location: Redmond, WA USA

Re: Converting PGN to CSV file

Post by Dann Corbit »

Jon12345 wrote:I've just install that program. it looks very tricky to run. Is it command line or something? Do I run it in a DOS window?
From the command line, like this:

pgn-extract -w10000 {pgn_file} > {pgn_one_line}
where {pgn_file} is the name of your pgn file and {pgn_one_line} is the name of the file where the PGN is spread out into one single line.
Jon12345
Posts: 80
Joined: Tue May 11, 2010 6:18 pm

Re: Converting PGN to CSV file

Post by Jon12345 »

I've downloaded the file and extracted the program into a folder, but how do I run it?
Jon
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: Converting PGN to CSV file

Post by Don »

Jon12345 wrote:I've downloaded the file and extracted the program into a folder, but how do I run it?
start by opening up a command shell. It is called "cmd.exe"

The program you want to run must either be in your "path" or in the same directory that you are in when cmd.exe comes up.

Then basically run it the way Dann layed out.