运行AHK脚本后更改CSV输出的格式

时间:2014-08-02 19:22:47

标签: csv automation autohotkey

我的代码存在问题[这里] [1]不确定为什么它会在[格式] [2]中输出我的csv,知道为什么?条目应该都在一行上,但它将它分成两行。感谢。

输入

;// Fileread, test, somefilename
fileread, AddressList, test.csv

IE := ComObjCreate("InternetExplorer.Application")
ComObjError(false)
IE.Visible := true

loop, parse, AddressList, `n
{
    PostCode := A_LoopField
    IE.Navigate("http://nominatim.openstreetmap.org/search.php?q=" PostCode)

    ToolTip, Now looking up %PostCode%

    while IE.readyState!=4 || IE.document.readyState != "complete" || IE.busy
        continue

    ; Collect results 1
    Sleep 2000

    ToolTip, ; remove tooltip

    Name_Elements       := IE.document.getElementsByClassName("name")
    Loop, 1
    {
        Addr_text       := Name_Elements[A_Index-1].innertext

        Latlon_element  := Name_Elements[A_Index-1].parentElement.getElementsByClassName("latlon")[0]
        Latlon_text     := Latlon_element.innertext

        String_Object   := StrSplit(Addr_text, "`,")
        LatLon_Object   := StrSplit(Latlon_text, "`,")

        If (Substr(Addr_text, 1, 2) = "MK")
        {
            Addr := trim(String_Object[2]) . "," . trim(String_Object[3]) . "," . PostCode . "," . LatLon_Object[1] . "," . LatLon_Object[2]
        }
        Else
        {
            Addr := trim(String_Object[1]) . "," . trim(String_Object[2]) . "," . PostCode . "," . LatLon_Object[1] . "," . LatLon_Object[2]
        }

        Filename_text   := Substr(PostCode, 1, 2)
        StringLower Filename_text, Filename_text

        FileAppend,%Addr%`n,%Filename_text%_addresslist.txt

    }
}
IE.quit()

输出

Levens Hall Drive,Westcroft,MK4 4FL
,52.003,-0.798
Slatepits Croft,Olney,MK46 5EF
,52.163,-0.708
Water Eaton Road,Water Eaton,MK2 2RD
,51.99,-0.728
Southern Way,Hodge Lea,MK12 5EG
,52.054,-0.811
Saxon Street,Stantonbury,MK13 7BX
,52.067,-0.776
Tongwell Street,Brinklow,MK9 2ZQ
,52.032,-0.692
,,MK17 0FE
,,
Bury Street,Green Park,MK16 8EU
,52.086,-0.726

所需输出

Levens Hall Drive,Westcroft,MK4 4FL,52.003,-0.798
Slatepits Croft,Olney,MK46 5EF,52.163,-0.708
Water Eaton Road,Water Eaton,MK2 2RD,51.99,-0.728
Southern Way,Hodge Lea,MK12 5EG,52.054,-0.811
Saxon Street,Stantonbury,MK13 7BX,52.067,-0.776
Tongwell Street,Brinklow,MK9 2ZQ,52.032,-0.692
,,MK17 0FE
,,
Bury Street,Green Park,MK16 8EU,52.086,-0.726

1 个答案:

答案 0 :(得分:2)

Windows文本文件通常有回车 `r作为附加行分隔符,因此文件看起来像Line 1`r`nLine 2`r`n
 Loop, Parse, myString, `n将在每个换行符处拆分字符串,但将回车符保留为A_LoopField的一部分。尽管如此,大多数编辑都将单独的回车解释为换行符 为了摆脱回车,StringSplit提供参数OmitChars“字符列表(区分大小写)以从每个子字符串的开头和结尾排除”< / em>的。
您唯一需要改变的是解析循环:

Loop, Parse, AddressList, `n, `r