找回密码
 加入
搜索
查看: 13738|回复: 33

[AU3基础] 用AU3怎样删除ini配置文件中重复的行?

 火... [复制链接]
发表于 2011-3-12 14:06:43 | 显示全部楼层 |阅读模式
本帖最后由 kood481748 于 2011-3-13 14:59 编辑

本人做了一个词库的配置文件,其中保存了几千个成语,难免会有重复的成语,
现在我想用AU3来检索这个配置文件中是否包含有重复的成语(重复的行),
如果有重复的行则自动删除重复的那一行,请问怎么删除ini配置文件中重复的行?
$i="0"
$p="1"

DO
        $mzbz = IniReadSection(@ScriptDir & "\XXX.ini", "config")
        do
                If $mzbz[1+$i][1] <> $mzbz[1+$p][1] Then
                        ToolTip("字符串1:"& $mzbz[1+$i][1] &@CR& "字符串2:"& $mzbz[1+$p][1] &@CR& "以上两个字符串不相同!如有重复将直接删除重复的行",@DesktopWidth-350, @DesktopHeight-130,"提示",1)
                        Sleep(1000)
                        $p = $p+1
                Else
                        ToolTip("字符串1:"& $mzbz[1+$i][1] &@CR& "字符串2:"& $mzbz[1+$p][1] &@CR& "字符串1 和字符串2 相同! 程序将删除字符串2这一行",@DesktopWidth-350, @DesktopHeight-130,"提示",1)

                EndIf
        until $p = $mzbz[0][0]

        $i = $i+1
        $p = $i+1

UNtil $p = $mzbz[0][0]

配置文件XXX.ini内容如下:
[config]
key=白骨精他妈
key=沙和尚
key=白骨
key=孙悟空
key=白骨精
key=白骨夫人
key=孙悟空

评分

参与人数 1金钱 +20 贡献 +5 收起 理由
tryhi + 20 + 5

查看全部评分

发表于 2011-3-12 14:30:59 | 显示全部楼层
#Include <File.au3>
$ini = "1.ini"
$line = _FileCountLines($ini)
For $i = 1 To $line
        $lines = FileReadLine($ini,$i)
        For $j = $i To $line
                If $lines = FileReadLine($ini,$j) Then MsgBox(0,0,"发现相同行")
        Next
Next

评分

参与人数 1贡献 +2 收起 理由
kood481748 + 2 谢谢老师指导

查看全部评分

发表于 2011-3-12 14:31:16 | 显示全部楼层
回复 1# kood481748


通过第三方文件中转一下,成么?学习中。。。。高手勿笑话。
#include 'File.au3'        
        $file =FileOpen(@ScriptDir&"\xxx.ini",0)
        _FileCreate(@ScriptDir&"\tmpxxx.ini")

If $file = -1 Then Exit(MsgBox(16,"test","读取本地的xxx.ini文件失败"))  
        For $i=1 To 10000
          $filereading=FileReadLine($file,$i)
            If @error =-1 Then 
                Exit(MsgBox(262144+64,"OK","全部处理完成!")) 
                EndIf
                   If Not StringInStr(FileRead (@ScriptDir&"\tmpxxx.ini"),$filereading) then
                     $filewrite =FileOpen(@ScriptDir&"\tmpxxx.ini",1)
                     FileWriteLine($filewrite ,$filereading)
                     FileClose($filewrite)
         EndIf
        Next
FileClose($file)

评分

参与人数 1贡献 +5 收起 理由
kood481748 + 5 谢谢老师指导,你的代码非常帅

查看全部评分

发表于 2011-3-12 14:41:47 | 显示全部楼层
本帖最后由 水木子 于 2011-3-12 14:47 编辑

$sText = FileRead('XXX.ini')
$sRer = StringRegExpReplace($sText, '(key=.+)(?=[^\1]+\1)', '')
$sRer = StringRegExpReplace($sRer, '^\v+|\r?\n(?=\r?\n)|\r?\n$', '') ;去掉之前遗留下来的空行!
MsgBox(0, '', $sRer)

评分

参与人数 2金钱 +20 贡献 +8 收起 理由
tryhi + 20 + 5 这个代码好帅
kood481748 + 3 谢谢老师指导,虽然我不太会用正则

查看全部评分

 楼主| 发表于 2011-3-12 15:11:49 | 显示全部楼层
感谢各位老师的指导,学生会继续研究……

3楼老师的代码非常帅
4楼水版的代码我不知道怎么用在我的代码中
发表于 2011-3-12 15:21:15 | 显示全部楼层
回复 5# kood481748

非得要发现重复的做提示后?再删除吗?
我这个只是直接帮你将整篇文本直接搞定,清除重复的内容。
发表于 2011-3-12 15:57:15 | 显示全部楼层
水木子 发表于 2011-3-12 14:41


又一个像A版的正则牛人出现。。。。。我最近在看那个网上广为流传的30分钟教程,糊里糊头的。。。唉。。。痛苦中。。。。不知水版有何教材及方法么/?感激不尽。。。。
发表于 2011-3-12 19:15:40 | 显示全部楼层
$file =FileOpen(@ScriptDir&"\xxx.ini",0)
If $file = -1 Then Exit(MsgBox(16,"test","读取本地的xxx.ini文件失败"))  
$dic=objcreate("scripting.dictionary")
if isobj($dic) then
        do
        $str=FileReadLine($file)
                If @error =-1 Then 
                        Exit(MsgBox(262144+64,"OK","全部处理完成!")) 
                EndIf
                If Not $dic.exists($str) then
                        $dic.add($str,"")
                else
                        msgbox(0,0,"发现重复内容:" & $str)
                EndIf
        until @error=-1
endif
FileClose($file)
一直想用字典的,今天终于用上一次

评分

参与人数 1贡献 +2 收起 理由
kood481748 + 2 谢谢你,经过测试,你的代码比3#和4#的代码 ...

查看全部评分

发表于 2011-3-12 19:44:57 | 显示全部楼层
谢谢分享!!学习了!!
发表于 2011-3-12 20:02:51 | 显示全部楼层
回复 7# boyhong

先了解多一些,再连接精一些!

教程只能让带你“进门”,师傅带进门,修行靠个人。
多做练习,举一反三!
 楼主| 发表于 2011-3-13 14:58:20 | 显示全部楼层
感谢各位老师的指点,经过我认真测试了一下,发现只有8楼“kevinch”的代码是最理想的,
3楼“boyhong”和4楼“水木子”的代码都有一点小问题,请看下图:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×
 楼主| 发表于 2011-3-13 15:06:29 | 显示全部楼层
本帖最后由 kood481748 于 2011-3-13 15:07 编辑

以下是8楼“kevinch”的代码(稍作修改),用来处理文件中的重复行比较理想
$file =FileOpen(@ScriptDir&"\XXX.ini",0)
If $file = -1 Then Exit(MsgBox(16,"test","读取本地的xxx.ini文件失败"))  
$dic=objcreate("scripting.dictionary")
if isobj($dic) then
        do
        $str=FileReadLine($file)
                If @error =-1 Then 
                        Exit(MsgBox(262144+64,"OK","全部处理完成!")) 
                EndIf
                If Not $dic.exists($str) then
                        $dic.add($str,"")
             $filewrite =FileOpen(@ScriptDir&"\处理后的文件.ini",1)
                     FileWriteLine($filewrite, $str)
                     FileClose($filewrite)
                else
                     $filewrite =FileOpen(@ScriptDir&"\重复的行.ini",1)
                     FileWriteLine($filewrite, $str)
                     FileClose($filewrite)
                EndIf
        until @error=-1
endif
FileClose($file)
发表于 2011-3-13 20:05:38 | 显示全部楼层
本帖最后由 netegg 于 2011-3-13 20:09 编辑
#cs
'file---1.ini'
[config]
key=白骨精他妈
key=沙和尚
key=白骨
key=孙悟空
key=白骨精
key=白骨夫人
key=孙悟空
#ce

#include<file.au3>
#include<array.au3>
Global $aA, $aB
_filereadtoarray('1.ini', $aA)
_Arraydelete($aA, 0)
$aB = _ArrayUnique($aA)
_Arraydelete($aB, 0)
_FileWriteFromArray('2.ini', $aB)
MsgBox(0,0,FileRead('2.ini'))

评分

参与人数 2金钱 +40 贡献 +2 收起 理由
kood481748 + 20 + 2 你的脚本也很不错,不过处理上万行文本速度 ...
3mile + 20 同感

查看全部评分

发表于 2011-3-13 23:18:46 | 显示全部楼层
你提的意见没错,就算用正则,处理起来也不简单,毕竟只是相同,没有指定格式
发表于 2011-3-14 00:15:28 | 显示全部楼层
回复 12# kood481748
如果上万行或数十万行的话,你这个效率也够呛.
蛋蛋兄提供的是一个相对成熟并且容易理解的方法.
既然提到效率,试试这个:
#include <File.au3>
 
$aFile = FileOpenDialog("快速删除重复行",@ScriptDir&"\","文本文件 (*.ini)", 1 + 4)
If @error Then 
        MsgBox(0,0,"文件未选择",3)
        Exit
EndIf
$begin = TimerInit()
Local $str='',$file_array
_FileReadToArray($aFile,$file_array)
;_ArrayDisplay($file_array)
For $i = 1 To UBound($file_array)-1
        $temp=$file_array[$i]
        If Not IsDeclared($temp) Then
                Assign($temp, $i)               
                $str&=$temp&@CRLF
        EndIf
Next
$dif = TimerDiff($begin)
        $out_file=StringTrimRight($aFile,4)&'_已完成.ini'
        if FileExists($out_file) then FileDelete($out_file)
        FileWrite($out_file,$str)
        ShellExecute($out_file)
        WinWaitActive("[CLASS:Notepad]")
        WinSetTitle("[CLASS:Notepad]", "", '处理'&UBound($file_array)&'行'&'     用时'&int($dif)&'毫秒')
Exit
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-17 04:28 , Processed in 0.087525 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表