 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
poo_noo
Joined: 08 Dec 2006 Posts: 66 Location: Sydney Australia
|
Posted: Thu May 15, 2008 5:46 am Post subject: [SOLVED] Convert CSV into HTML table |
|
|
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 |
|
 |
n-l-i-d Guest
|
Posted: Thu May 15, 2008 10:00 am Post subject: |
|
|
| 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
|
Posted: Thu May 15, 2008 12:59 pm Post subject: |
|
|
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 |
|
 |
HugoV
Joined: 27 May 2007 Posts: 378
|
Posted: Thu May 15, 2008 1:11 pm Post subject: |
|
|
| 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 |
|
 |
n-l-i-d Guest
|
Posted: Thu May 15, 2008 1:27 pm Post subject: |
|
|
| 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
|
Posted: Thu May 15, 2008 1:33 pm Post subject: |
|
|
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 |
|
 |
n-l-i-d Guest
|
Posted: Thu May 15, 2008 1:34 pm Post subject: |
|
|
| What exact error message do you get, and, did you try the second script? |
|
| Back to top |
|
 |
Oberon
Joined: 18 Feb 2008 Posts: 458
|
Posted: Thu May 15, 2008 1:37 pm Post subject: |
|
|
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 |
|
 |
poo_noo
Joined: 08 Dec 2006 Posts: 66 Location: Sydney Australia
|
Posted: Thu May 15, 2008 1:56 pm Post subject: |
|
|
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 |
|
 |
n-l-i-d Guest
|
Posted: Thu May 15, 2008 2:01 pm Post subject: |
|
|
| 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
|
Posted: Thu May 15, 2008 2:29 pm Post subject: |
|
|
| 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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|