CSV2HTML

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TronDoc.

I'm writing a converter because the ones I've found don't do
quite what I'd like them to do. Here is what I have so far.
It is functional but needs a lot of work.
Any suggestions are appreciated.
Thank You.

Joe B.
p.s. use as you wish...

Code: Select all

;-------------------------------------------------------------------------
; A simple CSV to HTML file converter
; works O.K., but has a bug with 
;"empty" data at the end of a line (e.g.:  3,test,)
; I hope to add coloring....
; any help appreciated!
;-------------------------------------------------------------------------
text$  = "by the elecTRONics DOCtor"           ; just a small text$ string
dlay2   = 2000                                 ; delay will set to 4000

OpenConsole()
ConsoleTitle ("TronDoc's CSV2HTML")

If ReadFile(0, "InPut.CSV")
  If OpenFile(1, "OutPut.html")
    WriteStringN("")
    CloseFile(1)
  Else
    ClearConsole()
    CloseConsole()
    MessageRequester("PureBasic", "Error: Can't read the file", 0)
    End
  EndIf
  
  While Eof(0)=0
    CSV$ =  StripTrail(StripLead(ReadString()))
    PrintN ("Working...")
    Gosub CSV2HTML 
    UseFile(0)
  Wend
  CloseFile(0)
Else
  MessageRequester("PureBasic", "Error: Can't read the file", 0)
EndIf

  If OpenFile(1, "OutPut.html")
    Length = Lof()
    FileSeek(Length)
    WriteStringN("")
    CloseFile(1)
  Else
    ClearConsole()
    CloseConsole()
    MessageRequester("PureBasic", "Error: Can't read the file", 0)
    End
  EndIf
  PrintN("DONE!")
Delay(dlay2)             ; Waits for moment
;-------- Cls --------
ClearConsole()            ; This will clean the ConsoleScreen
CloseConsole()
End

 CSV2HTML: 
 NewString$=""
 CSVlen = Len(CSV$)
 PrintN(Str(CSVlen))
 commaCount = 0
 PrevChar$=""
 For StartPosition = 1 To CSVlen
    CSVchar$ = Mid(CSV$, StartPosition, 1)
 If CSVchar$","
  ADDchar$ = CSVchar$
 Else
  If PrevChar$ = "," Or StartPosition = 1 Or StartPosition = CSVlen
   Addchar$ = " "
  Else
   AddChar$ = ""
  EndIf
 EndIf
    PrevChar$ = CSVchar$
    NewString$ = NewString$ + ADDchar$
 Next StartPosition
 NewString$=NewString$+""
 Gosub WriteHTML
 
 Return
 
 WriteHTML:
  If OpenFile(1, "OutPut.html")
    Length = Lof()
    FileSeek(Length)
    WriteStringN(NewString$)
    CloseFile(1)
  EndIf
 Return
 
 
;REM SAMPLE INPUT FILE:
;REM ------------InPut.CSV----------------
;REM DATE,TANDEM,W/VIDEO,,S/L,W/VIDEO,
;REM 2001.05.12,5,2,,20,1
;REM ,3,,4,1
;REM ,,,,,,
;REM 2001.05.13,0,0,0,0,,
;REM 2001.05.19,10,5,15,3,4
;REM --------------------------------------
 

Edited by - TronDoc on 25 April 2002 06:10:41
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.

I made a small program that uses an INI file to control the
table generation (incl. table attributes, column width/alignment)
If you are interested:

http://home.mnet-online.de/horst.muc/pb/csv2htm.zip

horst.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TronDoc.
I made a small program that uses an INI file to control the
table generation (incl. table attributes, column width/alignment)
If you are interested:
http://home.mnet-online.de/horst.muc/pb/csv2htm.zip
horst.
Hi Horst:
Many thanks! I can see that you put much more effort into your program.
It appears to work much better than mine. It handles empty arguments at
the end of a line better than mine though and it appears 100's of times
faster than mine!!
Joe B.


elecTRONics DOCtor
{registeredPB}P150 32Mb w98/DOS/Linux NO DirX NO IE :wink:

Edited by - TronDoc on 25 April 2002 18:38:50

Edited by - TronDoc on 25 April 2002 18:43:35
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by horst.
...and it appears 100's of times faster than mine!!
[:=] That's due to the FileBuffer.pbi
I never use ReadString() because it is very slow..

horst
Post Reply