将返回的 CSV 数据转换为 ColdFusion 中可搜索/可用的格式

发布于 2024-10-26 19:03:15 字数 383 浏览 1 评论 0原文

我有从 http://api.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IHAMPSHI46&month=3&day=25&year=2011&format=1 并需要将其转换为格式我可以根据第一列中的时间选择结果行之一。

转换为 struct/array/xml 会更好吗?实现这一目标的最佳方法是什么?

I have CSV data returned from http://api.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IHAMPSHI46&month=3&day=25&year=2011&format=1 and need to get it into a format where I can select one of the resulting rows based on the time in the first column.

Would it be better to convert to struct/array/xml and what would be the best way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遇见了你 2024-11-02 19:03:15

好的,我转储了结果,看起来 CSV 文件正在使用
标记来表示换行符,因此您必须进行自己的转换。以下是使用 cflib 中的 UDF 的示例:

<cfscript>
/**
* Transform a CSV formatted string with header column into a query object.
* 
* @param cvsString      CVS Data. (Required)
* @param rowDelim      Row delimiter. Defaults to CHR(10). (Optional)
* @param colDelim      Column delimiter. Defaults to a comma. (Optional)
* @return Returns a query. 
* @author Tony Brandner (tony@brandners.com)
* @version 1, September 30, 2005 
*/
function csvToQuery(csvString){
    var rowDelim = chr(10);
    var colDelim = ",";
    var numCols = 1;
    var newQuery = QueryNew("");
    var arrayCol = ArrayNew(1);
    var i = 1;
    var j = 1;

    csvString = trim(csvString);

    if(arrayLen(arguments) GE 2) rowDelim = arguments[2];
    if(arrayLen(arguments) GE 3) colDelim = arguments[3];

    arrayCol = listToArray(listFirst(csvString,rowDelim),colDelim);

    for(i=1; i le arrayLen(arrayCol); i=i+1) queryAddColumn(newQuery, arrayCol[i], ArrayNew(1));

    for(i=2; i le listLen(csvString,rowDelim); i=i+1) {
        queryAddRow(newQuery);
        for(j=1; j le arrayLen(arrayCol); j=j+1) {
            if(listLen(listGetAt(csvString,i,rowDelim),colDelim) ge j) {
                querySetCell(newQuery, arrayCol[j],listGetAt(listGetAt(csvString,i,rowDelim),j,colDelim), i-1);
            }
        }
    }
    return newQuery;
}
</cfscript>

<cfset u = "http://api.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IHAMPSHI46&month=3&day=25&year=2011&format=1" />

<cfhttp url="#u#" result="csv" />
<!--- if you dump the result you can see that the result contains <br> tags to indicate new lines --->
<!--- 
<cfdump var="#csv.fileContent#" />
--->
<cfdump var="#csvToQuery(replace(csv.fileContent, "<br>", "", "all"))#" />

OK, I dumped the result and it looks like the CSV file is using <br> tags to denote a newline so you'll have to roll your own conversion. Here's an example that uses a UDF from cflib:

<cfscript>
/**
* Transform a CSV formatted string with header column into a query object.
* 
* @param cvsString      CVS Data. (Required)
* @param rowDelim      Row delimiter. Defaults to CHR(10). (Optional)
* @param colDelim      Column delimiter. Defaults to a comma. (Optional)
* @return Returns a query. 
* @author Tony Brandner (tony@brandners.com)
* @version 1, September 30, 2005 
*/
function csvToQuery(csvString){
    var rowDelim = chr(10);
    var colDelim = ",";
    var numCols = 1;
    var newQuery = QueryNew("");
    var arrayCol = ArrayNew(1);
    var i = 1;
    var j = 1;

    csvString = trim(csvString);

    if(arrayLen(arguments) GE 2) rowDelim = arguments[2];
    if(arrayLen(arguments) GE 3) colDelim = arguments[3];

    arrayCol = listToArray(listFirst(csvString,rowDelim),colDelim);

    for(i=1; i le arrayLen(arrayCol); i=i+1) queryAddColumn(newQuery, arrayCol[i], ArrayNew(1));

    for(i=2; i le listLen(csvString,rowDelim); i=i+1) {
        queryAddRow(newQuery);
        for(j=1; j le arrayLen(arrayCol); j=j+1) {
            if(listLen(listGetAt(csvString,i,rowDelim),colDelim) ge j) {
                querySetCell(newQuery, arrayCol[j],listGetAt(listGetAt(csvString,i,rowDelim),j,colDelim), i-1);
            }
        }
    }
    return newQuery;
}
</cfscript>

<cfset u = "http://api.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IHAMPSHI46&month=3&day=25&year=2011&format=1" />

<cfhttp url="#u#" result="csv" />
<!--- if you dump the result you can see that the result contains <br> tags to indicate new lines --->
<!--- 
<cfdump var="#csv.fileContent#" />
--->
<cfdump var="#csvToQuery(replace(csv.fileContent, "<br>", "", "all"))#" />
仙气飘飘 2024-11-02 19:03:15

查看 cfhttp 的文档。使用 name 属性,您将从 http 调用中得到一个查询。然后你可以对结果进行查询的查询。

Check the docs for cfhttp. Use the name attribute and you'll get a query back from the http call. Then you can do a query of queries on the result.

画离情绘悲伤 2024-11-02 19:03:15

您的 CSV 结果使用 < br>而不是换行符。
使用此 cflib 函数,http://www.cflib.org/udf/CSVToQuery

Your CSV result is using < br > instead of linebreak.
Use this cflib function, http://www.cflib.org/udf/CSVToQuery

雪落纷纷 2024-11-02 19:03:15

使用 H2 数据库 (www.h2database.com),您可以使用方便的 csvread 函数。
http://cfstuff.blogspot.com/2009/06 /using-h2-database-in-coldfusion.html

<cfset u="http://api.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IHAMPSHI46&month=3&day=27&year=2011&format=1"/>

<cfhttp url="#u#" result="csv" />

<cffile 
    action="write" 
    file="#ExpandPath('./w3.csv')#" 
    output="#rereplace(csv.fileContent, "<br>|<!--.*-->", "", "all")#" />


<cfquery name="w2" datasource="h3"> 
    select * from csvread('#ExpandPath('./w3.csv')#')

</cfquery>
<cfdump var=#w2# expand="no"/>

<cfset yesterday = dateadd("d",-1,now())/>
<cfquery name="w2" datasource="h3"> 

    SELECT Time, TemperatureF, WindSpeedMPH FROM csvread('#ExpandPath('./w3.csv')#')
    WHERE  Time BETWEEN '#dateFormat(yesterday, "YYYY-MM-DD")# #timeFormat(dateAdd("h", -1, yesterday), "HH:MM")#:00' AND '#dateFormat(yesterday, "YYYY-MM-DD")# #timeFormat(dateAdd("h", 1, yesterday), "HH:MM")#:00' 
</cfquery>


<cfdump var=#w2#/>

Using an H2 database (www.h2database.com) you can use the ever handy csvread function.
http://cfstuff.blogspot.com/2009/06/using-h2-database-in-coldfusion.html

<cfset u="http://api.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IHAMPSHI46&month=3&day=27&year=2011&format=1"/>

<cfhttp url="#u#" result="csv" />

<cffile 
    action="write" 
    file="#ExpandPath('./w3.csv')#" 
    output="#rereplace(csv.fileContent, "<br>|<!--.*-->", "", "all")#" />


<cfquery name="w2" datasource="h3"> 
    select * from csvread('#ExpandPath('./w3.csv')#')

</cfquery>
<cfdump var=#w2# expand="no"/>

<cfset yesterday = dateadd("d",-1,now())/>
<cfquery name="w2" datasource="h3"> 

    SELECT Time, TemperatureF, WindSpeedMPH FROM csvread('#ExpandPath('./w3.csv')#')
    WHERE  Time BETWEEN '#dateFormat(yesterday, "YYYY-MM-DD")# #timeFormat(dateAdd("h", -1, yesterday), "HH:MM")#:00' AND '#dateFormat(yesterday, "YYYY-MM-DD")# #timeFormat(dateAdd("h", 1, yesterday), "HH:MM")#:00' 
</cfquery>


<cfdump var=#w2#/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文