AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[SOLVED] Convert CSV into HTML table

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
poo_noo



Joined: 08 Dec 2006
Posts: 66
Location: Sydney Australia

PostPosted: Thu May 15, 2008 5:46 am    Post subject: [SOLVED] Convert CSV into HTML table Reply with quote

Hi all

I am stumped on this and I hope others can help. I have a CSV file that I would like to turn into a HTML table.

Each line in the CSV has 10 fields.

I would like to wrap each line in a <tr></tr> tag.

For each field in a line I would like to wrap in a <td></td>.

I have adapted the code from http://www.autohotkey.com/forum/viewtopic.php?t=8154&start=0&postdays=0&postorder=asc&highlight=html

Code:
#SingleInstance, force
SetBatchLines, -1
 Loop, read, attach_20080514_cba_2_bts_adjustments.csv
 {
   Loop, parse, A_LoopReadLine,CSV
      tr_file=
      (LTrim
      %tr_file%
      <tr>
      <td>%A_LoopField%</td>
      <td>%A_LoopField%</td>
      <td>%A_LoopField%</td>
      <td>%A_LoopField%</td>
      <td>%A_LoopField%</td>
      <td>%A_LoopField%</td>
      <td>%A_LoopField%</td>
      <td>%A_LoopField%</td>
      <td>%A_LoopField%</td>
      <td>%A_LoopField%</td>      
      </tr>`r`n
      )
}

time=%A_Now%

FileAppend,
(
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Test123</title>
</head>
<body bgcolor="white" text="black" link="black" vlink="black" alink="red">
<table border="0" cellspacing="2" cellpadding="2">
<tr bgcolor="#B2AD96">
<th align="left">Field1</th>
<th align="left">Field2</th>
<th align="left">Field3</th>
<th align="left">Field4</th>
<th align="left">Field5</th>
<th align="left">Field6</th>
<th align="left">Field7</th>
<th align="left">Field8</th>
<th align="left">Field9</th>
<th align="left">Field10</th>
</tr>
%tr_file%
</table>
</body>
</html>
), dirlist_%time%.html

Run, dirlist_%time%.html


The code above repeats the field values on each line. Any help in pointing me in the right direction is greatly appreciated.

Thanks in advance
_________________
Paul O


Last edited by poo_noo on Thu May 15, 2008 11:33 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
n-l-i-d
Guest





PostPosted: Thu May 15, 2008 10:00 am    Post subject: Reply with quote

Code:

#SingleInstance, force
SetBatchLines, -1
Loop, Read, attach_20080514_cba_2_bts_adjustments.csv
{
   Loop, Parse, A_LoopReadLine, CSV
   {
      td_fields .= td_fields . "<td>" . A_LoopField . "</td>`n"
   }
   tr_fields .= tr_fields . "<tr>" . td_fields . "</tr>`n"
}
time=%A_Now%

FileAppend,
(
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Test123</title>
</head>
<body bgcolor="white" text="black" link="black" vlink="black" alink="red">
<table border="0" cellspacing="2" cellpadding="2">
<tr bgcolor="#B2AD96">
<th align="left">Field1</th>
<th align="left">Field2</th>
<th align="left">Field3</th>
<th align="left">Field4</th>
<th align="left">Field5</th>
<th align="left">Field6</th>
<th align="left">Field7</th>
<th align="left">Field8</th>
<th align="left">Field9</th>
<th align="left">Field10</th>
</tr>
%tr_fields%
</table>
</body>
</html>
), dirlist_%time%.html

Run, dirlist_%time%.html



This (not tested)?
Back to top
poo_noo



Joined: 08 Dec 2006
Posts: 66
Location: Sydney Australia

PostPosted: Thu May 15, 2008 12:59 pm    Post subject: Reply with quote

Hi & thanks but that maxed out. I got a memory limit reached error.

But I might try it using regular expressions.

i.e. replace the start of line with a <tr><td>, each comma with a </td><td> and the line end with a </td></tr>

Thanks
_________________
Paul O
Back to top
View user's profile Send private message Visit poster's website
HugoV



Joined: 27 May 2007
Posts: 378

PostPosted: Thu May 15, 2008 1:11 pm    Post subject: Reply with quote

Why not take n-l-i-ds script and FileAppend each TR INSIDE the Loop, that way it might a bit slower but tr_fields won't use as much memory (only one table row per loop)
Back to top
View user's profile Send private message
n-l-i-d
Guest





PostPosted: Thu May 15, 2008 1:27 pm    Post subject: Reply with quote

Code:
#SingleInstance, force
SetBatchLines, -1

time=%A_Now%

inputFile := A_ScriptDir . "/attach_20080514_cba_2_bts_adjustments.csv"
outputFile := A_ScriptDir . "/dirlist_" . time . ".html"

FileAppend,
(
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="content-type">
<title>Test123</title>
</head>
<body bgcolor="white" text="black" link="black" vlink="black" alink="red">
<table border="0" cellspacing="2" cellpadding="2">
<tr bgcolor="#B2AD96">
<th align="left">Field1</th>
<th align="left">Field2</th>
<th align="left">Field3</th>
<th align="left">Field4</th>
<th align="left">Field5</th>
<th align="left">Field6</th>
<th align="left">Field7</th>
<th align="left">Field8</th>
<th align="left">Field9</th>
<th align="left">Field10</th>
</tr>
), %outputFile%

Loop, Read, %inputFile%, %outputFile%
{
     FileAppend, <tr>`n
   Loop, Parse, A_LoopReadLine, CSV
   {
      FileAppend, % "<td>" . A_LoopField . "</td>`n"
   }
   FileAppend, </tr>`n
}

FileAppend,
(
</table>
</body>
</html>
), %outputFile%

Run, %outputFile%


This (again: not tested)?
Back to top
poo_noo



Joined: 08 Dec 2006
Posts: 66
Location: Sydney Australia

PostPosted: Thu May 15, 2008 1:33 pm    Post subject: Reply with quote

Thanks for the reply HugoV.

Could you provide an example ? I still get memory errors.

Thanks in advance.

EDITED. scrub this post. n-l-i-d posted a reponse while I was typing. Thanks
_________________
Paul O


Last edited by poo_noo on Thu May 15, 2008 1:36 pm; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website
n-l-i-d
Guest





PostPosted: Thu May 15, 2008 1:34 pm    Post subject: Reply with quote

What exact error message do you get, and, did you try the second script?
Back to top
Oberon



Joined: 18 Feb 2008
Posts: 458

PostPosted: Thu May 15, 2008 1:37 pm    Post subject: Reply with quote

You can also use regex:

Code:
csv = ; complex example from wikipedia
(
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""",,4900.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00
)

html := csv
Loop
   If (!(html := RegExReplace(html, "(""[^,""]*?),", "$1,", c)) or !c)
      Break
html := RegExReplace(html, "`n", "</tr><tr>")
html := RegExReplace(html, ",", "</td><td>")
StringReplace, html, html, ,, `,, All
html = <tr><td>%html%</td></tr>

MsgBox, %html%
Back to top
View user's profile Send private message
poo_noo



Joined: 08 Dec 2006
Posts: 66
Location: Sydney Australia

PostPosted: Thu May 15, 2008 1:56 pm    Post subject: Reply with quote

Thanks Oberon

I tried your code but got an error
Code:
---------------------------
Document3.ahk
---------------------------
Error at line 15.

Line Text: StringReplace, html
Error: "StringReplace" requires that parameter #3 be non-blank.

The program will exit.
---------------------------
OK   
---------------------------


Thanks anyway and thanks to all who have helped me. Much appreciated.
_________________
Paul O
Back to top
View user's profile Send private message Visit poster's website
n-l-i-d
Guest





PostPosted: Thu May 15, 2008 2:01 pm    Post subject: Reply with quote

Try copying the code from the forum directly by selecting/copying instead of using the JavaScript "copy" link. The errors you describe might have something to do with that.
Back to top
Oberon



Joined: 18 Feb 2008
Posts: 458

PostPosted: Thu May 15, 2008 2:29 pm    Post subject: Reply with quote

poo_noo wrote:
I tried your code but got an error
The forum derefed my chars so try this:

Code:
csv = ; complex example from wikipedia
(
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""",,4900.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00
)

html := csv
Loop
   If (!(html := RegExReplace(html, "(""[^,""]*?),", "$1__c__", c)) or !c)
      Break
html := RegExReplace(html, "`n", "</tr><tr>")
html := RegExReplace(html, ",", "</td><td>")
StringReplace, html, html, __c__, `,, All
html = <tr><td>%html%</td></tr>

MsgBox, %html%
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group