【问题标题】:Autohotkey reading a csv file with million linesAutohotkey 读取具有百万行的 csv 文件
【发布时间】:2017-12-16 08:58:28
【问题描述】:

我的要求是通过 grepping 特定字符串并显示输出行来读取大约百万行的 CSV 文件。

CSV 文件示例:

Rob,school,oxford,tennis
James,school,cambridge,cricket
lucy,college,harvard,football
lily,hotel,novotel,golf
..
..
..
1 million lines.

要求:

当我调用 AHK 脚本时,它会通过 InputBox 提示用户输入,并将输入作为 James school 并输出为 剑桥。同样输入为lily hotel,它应该输出为novotel

我能够通过下面的脚本实现所需的输出,但问题是当我搜索一个字符串时,例如在第 1 百万行大约需要 5-10 分钟 给我输出。

我写的脚本:

#SingleInstance, force
#Include C:\Users\mpechett\Desktop\ahk\tf.ahk

InputBox, Name, Search for Name


StringSplit, word_array, Name, %A_Space%, .  ; Omits periods.

pattern = %word_array1%,%word_array2%


Outputline = % TF_Find("C:\Users\mpechett\example.csv", "","", pattern, 1, 1)


MsgBox,%Outputline%

请帮助我提高脚本的性能。

【问题讨论】:

  • 我会将sort 的内容按字母顺序排列,然后将split 这个大文件分成几个较小的文件(a.csv、b.csv 等)。
  • 这是一种方法。不分文件,有什么办法可以提高速度吗?

标签: scripting autohotkey


【解决方案1】:

这是索引数据库类型解决方案的伪代码:

make_index() {
  global file := FileOpen( "database.csv", "r" )
  for each line in database {
    position  := file.pos
    line      := file.readline()
    values    := StrSplit(line)
    key       := make_a_unique_key(values)
    hash[key] := position
  }
  save hash to "database.index"
}

lookup(values) {
  global file, hash
  file.seek( hash[ make_a_unique_key( values )])
  return file.readline()
}

请参阅:File object 的手动输入

【讨论】:

    【解决方案2】:

    如果您使用 RAMDISK,您可以加快搜索结果的速度。我看不到你的 tf.ahk 脚本是什么,但是 5-10 分钟太长了,1 - tf.ahk 文件中的循环代码不好 2 - 或者每次从你的硬盘 c 中搜索时它都会这样做:而不是来自你的内存。

    您可以从这里下载免费软件Imdisk

    • RAMDISK 是一个虚拟硬盘,放置在您的 RAM 内存中。

    • RAMDISK 比硬盘快 +-100 倍。

    首先在您的 Windows 系统上安装 IMDISK,然后您可以简单地安装/放置/复制 任何应用程序 或将任何 csv 文件 放置/复制到 Ramdisk [示例 - z :\example.csv]

    z:\example.csv

    Rob,school,oxford,tennis
    James,school,cambridge,cricket
    lucy,college,harvard,football
    lily,hotel,novotel,golf
    ..
    ..
    ..
    1 million lines.
    

    注意:使用此 AHK 键盘快捷键宏脚本,您可以输入 - 例如:college,harvard,然后它将在记事本中进行搜索,并给出结果值 football (这只是一个测试示例,对于更大的文件,您需要稍微修改一下代码并使用其他可以处理更大文件的应用程序(SpeadSheat program))

    搜索.ahk

    ; this Script works on Windows 10 system.
    ; You can Click key, F1 to EXIT
    
    #SingleInstance, force
    
    run notepad.exe z:\example.csv
    WinWaitActive,example.csv, , 2
    
    
    
    loop
    {
    
    InputBox,Clipboard,Search for Name
    sleep 100
    send ^{Home} ;goto Top of the Page
    sleep 100
    send ^f ;goto the Find box 
    sleep 100
    send ^v ;paste Clipboard Value
    sleep 100
    send {enter}
    sleep 1500 ;You can change this sleep codeline - How bigger the search, how larger the sleep must be.
    send {esc}
    sleep 100
    
    ;If you want to Select the Whole Search Line - you can use this code.
    ;send {Home}
    ;sleep 100
    ;send +{End}
    
    ;If you want to Select the Rigth Site of the Line - you can use this code. 
    send {Right}
    sleep 100
    send +{End}
    
    sleep 100
    send ^c ;copy the Search LineValue to Clipboard 
    LineValue = %Clipboard%
    sleep 100
    word_array := StrSplit(LineValue, ",")
    sleep 100
    SearchValue := word_array[1]" "       ;word_array[2]" "word_array[3]
    sleep 100
    MsgBox "SearchValue",%SearchValue%
    }
    
    F1::ExitApp
    

    【讨论】:

      猜你喜欢
      • 2016-07-20
      • 2014-05-18
      • 2019-12-19
      • 2014-01-04
      • 2016-08-22
      • 2011-01-04
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      相关资源
      最近更新 更多