【问题标题】:Losing Columns when Replacing NULL values in Tab Delimited File - Coldfusion替换制表符分隔文件中的 NULL 值时丢失列 - Coldfusion
【发布时间】:2013-09-16 16:52:50
【问题描述】:

我有一个制表符分隔的文本文件,其中包含可怕的“NULL”值。经过一番搜索,我发现如何用“某物”替换空字符串……。但是当我这样做时,我似乎失去了一半的价值观。

<cfset thisRow = replace(thisRow, "#chr(9)##chr(9)#", "#chr(9)#ScoobySnack#chr(9)#", "all")>

例如:一行数据包含 120 个“空标签” - 但是当我执行此解决方法时 - 我只返回 60 个。代码片段在这里:

    <cffile action="read" file="c:\websites\the website\UsageData\#MyFile.NAME#" variable="myDataFile">

  <cfset rowCount = listLen(myDataFile,chr(13))>


  -Other stuff happens-


  <cfloop from="1" to="#rowCount#" index="i">
  <table cellpadding="2" cellspacing="0" border="1">
  <tr>
  <th>ID</th>
  <th>Date</th>
  <th>Time</th>
  <th>DeviceMac</th>
  <th>DeviceAddress</th>
  <th>DeviceName</th>
  <th>UsageBytes</th>
  </tr>
  <!--- set current row to simple variable --->
  <cfset thisRow = listGetAt(myDataFile,i,chr(13))>

  <cfset Field1 = listGetAt(thisRow,1,chr(9))>
  <cfset Field2 = listGetAt(thisRow,2,chr(9))> 
  <cfset Field3 = listGetAt(thisRow,3,chr(9))>


  <cfset thisRow = replace(thisRow, "#chr(9)##chr(9)#", "#chr(9)#ScoobySnack#chr(9)#", "all")>

  <cfset variables.CheckColumnCount = listLen(thisRow,chr(9))>

  <cfif variables.CheckColumnCount NEQ variables.ColumnCount>
  <cfset variables.AdjustedColumnCount = (variables.CheckColumnCount - 3)>
  <cfelse>
  <cfset variables.AdjustedColumnCount = (variables.ColumnCount - 3)>
  </cfif>

  <!---Row #i# Data as a string (#thisRow#)<br/>--->
  <cfset ColNum = 4>
  <!--- now loop the row --->
  <cfloop from="1" to="#variables.AdjustedColumnCount#" index="r">
  <cfif r EQ 1>
  <cfset variables.MyLoopStartTime = variables.MyStartTime>
  <cfelse>

  </cfif>

  <tr>
  <td>#ColNum#</td>
  <td>#variables.thisDate#</td>
  <td>#TimeFormat(variables.MyLoopStartTime,"hh:mm:ss")#</td>
  <td>#Field1#</td>
  <td>#Field2#</td>
  <td>#Field3#</td>
  <td>#listGetAt(thisRow,ColNum,chr(9))#</td>
  </tr>
  <cfset variables.MyLoopStartTime = DateAdd('n', 1, variables.MyLoopStartTime)>
  <cfset ColNum = ColNum +1>
  </cfloop>
  </table>
  <hr>
  <cfset count = count + 1>
  </cfloop>

想法?

【问题讨论】:

  • (编辑)如果你必须自己动手,不要使用replace()。使用数组更简单,特别是listToArray(..) with includeEmptyFields=true。也就是说,有better tools for this job。专门为解析分隔文件而设计的。
  • 谢谢 Leigh - 所以我的问题的答案是:&lt;cfset MyDataArray = ListToArray(listGetAt(myDataFile,i,chr(13)), chr(9), true)&gt;
  • 不完全。如果要切换到数组,请摆脱所有列表函数。您只需将其转换为数组一次。然后,在您使用列表的任何地方,请改用数组。代替listGetAt( yourList, x, delim ),使用yourArray[ x ],代替listLen( yourList, delim ),使用arrayLen( yourArray) ,等等。
  • 嗯 - 说实话 - 数组不是我的强项。当我将文件转换为数组时 - 它不会分隔行......这一切都变成了一大列。 (有 37000 行......)但我上面的代码确实将一行转换为一个数组并保留了空字段......(这很棒)但我知道我在混合......只是不知道如何保持原始文件中的行。

标签: list coldfusion replace tabs delimited


【解决方案1】:

(来自 cmets ..)

better tools for the job:专门为解析分隔文件而设计的。但是,要回答您的问题,如果您必须自己动手,请不要使用replace()。使用数组更简单,特别是listToArray()includeEmptyFields=true

读入文件,并在 CR 上拆分内容以创建行数组:

<!--- split content into an array of rows --->
<cfset rowArray  = listToArray(myDataFile, chr(13), true)>
<cfset rowCount  = arrayLen(rowArray)>

然后循环遍历数组,并从每一行创建一个单独的列数组:

<cfloop from="1" to="#rowCount#" index="rowIndex">
    <!--- split current row into an array of columns --->
    <cfset thisRow  = rowArray[ rowIndex ]>
    <cfset colArray = listToArray(thisRow , chr(9), true)>

    <!--- get first 3 columns (for illustration) --->   
    <cfset Field1 = colArray[ 1 ]>
    <cfset Field2 = colArray[ 2 ]> 
    <cfset Field3 = colArray[ 3 ])>
    ...

</cfloop>

如果你不需要循环索引来做其他事情,你也可以使用数组循环来代替:

<!--- split content into an array of rows --->
<cfset rowArray  = listToArray(myDataFile, chr(13), true)>
<cfloop array="#rowArray#" index="thisRow">
    <!--- split current row into an array of columns --->
    <cfset columns = listToArray(thisRow, chr(9), true)>

    <!--- get first N columns for illustration ---> 
    <cfset Field1 = columns [ 1 ]>
    <cfset Field2 = columns [ 2 ]> 
    <cfset Field3 = columns [ 3 ])>
    ...
</cfloop>

【讨论】:

    猜你喜欢
    • 2016-11-01
    • 2017-02-12
    • 2021-04-29
    • 2011-10-02
    • 1970-01-01
    • 2016-06-06
    • 2020-02-09
    • 2017-02-24
    • 1970-01-01
    相关资源
    最近更新 更多