Sign in to follow this  
Followers 0
daxle

Parsing Text File

4 posts in this topic

Hi there everyone,

I have a text file that looks something like the following:

Administrator        Guest               Matt                   
jaes                     james               jas                    
jasdfs               jasds               js                 
Matthew              pjaasddasdfs            pjaasdds               
pjads                pjdfs

Notice the odd formatting; there are not always a consistant amount of spaces between terms, I'm looking for a way to break up this text file into the individual terms (in this case user accounts), and not include the spaces. Any ideas?

Thanks for any advice!

Matt

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

If it's at least "consistant" with two or more spaces separating the columns, below will work.

The example returns a two dimensional array... rows ->[n][n]<- columns

#include <Array.au3> ; for _arraydisplay() func

Global $gs_somefile = "somefile_here.txt"
Global $ga_parsed = _myparse_filefunc($gs_somefile)

_ArrayDisplay($ga_parsed)

Func _myparse_filefunc($s_file)

    Local $s_fread = $s_file
    If FileExists($s_file) Then $s_fread = FileRead($s_file)

    If $s_fread = "" Then Return SetError(1, 0, 0)

    Local $a_lines = StringSplit(StringStripCR($s_fread), @LF)

    Local $a_col = 0
    Local $i_cols = 1, $i_rows = 0, $i_ub
    Local $a_wret[$a_lines[0] + 1][$i_cols]
    For $iline = 1 To $a_lines[0]
        If StringLen(StringStripWS($a_lines[$iline], 8)) = 0 Then ContinueLoop
        $a_col = StringRegExp($a_lines[$iline], "(?s)(.+?)(?:z|s{2,})", 3)
        If @error Then ContinueLoop
        $i_ub = UBound($a_col)
        If $i_ub > $i_cols Then
            $i_cols = $i_ub
            ReDim $a_wret[$a_lines[0] + 1][$i_cols]
        EndIf
        For $iword = 0 To $i_ub - 1
            $a_wret[$i_rows][$iword] = $a_col[$iword]
        Next
        $i_rows += 1
    Next

    If Not $i_rows Then Return SetError(2, 0, 0)

    ReDim $a_wret[$i_rows][$i_cols]
    Return $a_wret
EndFunc
Edited by SmOke_N

[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

#include <Array.au3>

$file = @ScriptDir & "test.txt"
$data = FileRead($file)
$data = StringStripWS($data, 7)
$array = StringSplit($data, " " & @CR)

_ArrayDisplay($array)

Edited by Spiff59
1 person likes this

Share this post


Link to post
Share on other sites

Thanks guys, both work fine!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • shotiko
      _GDIPlus and Buttons gone after minimize restore
      By shotiko
      i have a code that creates buttons and graphics in the same block.  before the block is _GDIPlus_Startup()
      $hGraphic = _GDIPlus_GraphicsCreateFromHWND($mainGUI) $hBrush = _GDIPlus_BrushCreateSolid(0xF03FC744) ; Felish Green colour $hPen = _GDIPlus_PenCreate(0xFFBFCC49); Granite colour $hPath = _GDIPlus_PathCreate() ;Create new path object $hFamily = _GDIPlus_FontFamilyCreate("Tahoma") ;Create font family object $tLayout = _GDIPlus_RectFCreate($startX, $startY, $bHeigh, $bWidth) ;Create string bounding rectangle X=0, Y=0 $hFormat = _GDIPlus_StringFormatCreate(0x0400); Allow overhanging rectangle _GDIPlus_StringFormatSetAlign($hFormat, 1); Align text horizontally _GDIPlus_StringFormatSetLineAlign($hFormat, 1) ; Align text Vertically ; Add String like CtrlLabels assign to rectangle and formats including Font _ _GDIPlus_PathAddString($hPath, FileReadLine($configF, $SN), $tLayout, $hFamily, 0, 26, $hFormat) _GDIPlus_GraphicsSetSmoothingMode($hGraphic, $GDIP_SMOOTHINGMODE_HIGHQUALITY) ;Sets the graphics object rendering quality (antialiasing) _GDIPlus_GraphicsFillPath($hGraphic, $hPath, $hBrush) ; Fill path to graphics handle (GUI) end of the block
      _GDIPlus_BrushDispose($hBrush) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_PenDispose($hPen) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_Shutdown() now this works on the first run but when i minimize and then restore _GDIPlus_ is gone and so are buttons. but if i create labels they stay.
      i've tried moving "Dispose" functions at the end of the loop but still the same result
       
    • Wicked_Caty
      Problems with arrays
      By Wicked_Caty
      In my current project I have a part that should convert from string to array, and later on that array should be converted to a string again.
      The first part works just fine and I get the array just as I want it, but on the way back I'm encountering some issues...
      Func Encode($data0) ; works just fine, just here for clarification. ; That if-statement in the middle is just there that I'm always getting a string with 5 symbols ; $data0 = "A" in this example Local $data1 = "" Local $tmp = "" Local $arr = StringSplit($data0, "", 2) For $i = 0 To UBound($arr)-1 $tmp = AscW($arr[$i]) If $tmp = 0 Then $tmp = "00000" & $tmp ElseIf $tmp > 0 And $tmp < 10 Then $tmp = "0000" & $tmp ElseIf $tmp >= 10 And $tmp < 100 Then $tmp = "000" & $tmp ElseIf $tmp >= 100 And $tmp < 1000 Then $tmp = "00" & $tmp ElseIf $tmp >= 1000 And $tmp < 10000 Then $tmp = "0" & $tmp ElseIf $tmp >= 10000 And $tmp <= 65535 Then $tmp = $tmp EndIf $data1 = $data1 & $tmp Next Return $data1 EndFunc Func Decode($data0) ; does NOT work ; $data0 = "00065" ; via MsgBox and ConsoleWrite I could find out that it fails already before StringSplit ; when it fails, no message is given and everything just disappears Local $data1 = "" Local $tmp = "" Local $arr = StringSplit(String($data0), "", 2) For $i = 0 To UBound($arr)-1 Step 5 $tmp = $arr[$i] & $arr[$i+1] & $arr[$i+2] & $arr[$i+3] & $arr[$i+4] & $arr[$i+5] $tmp = ChrW($tmp) $data1 = $data1 & $tmp Next Return $data1 EndFunc Sorry for dropping all the #include, the GUI, the login screen, etc... It's not necessary for this problem and I needn't give you a 613kB file full of plain text for finding an error in 3 lines of code.
      I found out that $arr is always empty, so there must be an issue with StringSplit. I suspected "00065" might be seen as an integer and converted it to a string, but that doesn't make it work. I already played around quite a while with that issue now, I hope some of you can finally relieve me.
      Thanks
    • Wicked_Caty
      Problem with FileCopy()
      By Wicked_Caty
      I'm writing a small program for creating a backup. I want to do that by using FileCopy. Everything from drive C should be copied to drive D.
      FileCopy("C:", "D:", $FC_OVERWRITE + $FC_CREATEPATH) That doesn't do anything, though. I played a bit with the paths, but nothing does what I want. Sometimes only the folder of the program itself is copied, sometimes nothing is copied.
      What am I doing wrong? What do I have to do that everything from C goes to D? Thanks!
      Backup.au3
      Edit: DirCopy does pretty much the same
    • Reizvoller
      BIG String to Array
      By Reizvoller
      Greetings,
      I am getting the "RawData" return from FastFind's "FFGetRawData" function and then attempting to make it into an array.
      What I have here does the job as it should but it is a slow process. I feel I may be overlooking a more effecient way to handle this matter. In the end ( in an ideal situation ) I'd like to end up with a 2D array with 580 col and 375 rows. Currently it's just a 1D array as I am still trying to get my head around how to make it into a 2D. BUT... My question is this ...
      Is there a better way to handle what I am trying to do? 
      Func trimmer () Local $i = 0 ;The "Counter" Local $WorkAreaLength = StringLen ($WorkAreaData) ;Get the length of the string ConsoleWrite ("WorkAreaLegnth="&$WorkAreaLength&@CR) Local $iWorkAreaLengthCount = $WorkAreaLength / 8 ;How many segments of 8 can we make? Use this for the counter later on ConsoleWrite ("WorkArealengthCount="&$iWorkAreaLengthCount&@CR) Local $sPiece = 0 ; The string segment holder Global $aWorkArea ; The array to write to Do $sPiece = StringLeft ($WorkAreaData, 8) ; Grab the first 8 characters and put them into $sPiece _ArrayAdd ($aWorkArea, $sPiece) ; Put what $sPiece is into the array StringTrimLeft ($WorkAreaData, 8) ;Trim off the first 8 characters that were just copied $i = $i + 1 ;Count up 1 ConsoleWrite ($i&@CR) Until $i >= $iWorkAreaLengthCount ; Keep going until $i >= $iWorkAreaLengthCount ConsoleWrite ("Done"&@CR) Local $sFile = FileOpen ("main.txt");Open the text file _FileWriteFromArray ($sFile , $aWorkArea);Write the array to the text file FileClose ($sFile); Close the text file EndFunc  
      A little secondary problem, that isn't throwing any errors to speak of, is the file is never written to. It remains blank even though it is in the same directory as the script and the script isn't responding with any errors. This isn't my main focus though as it just lets me see what the array is rather than using the console which can be a little cramped at times.
      Any advise would be appreciated, thank you!
    • akomax
      How to add/refresh a text after clicking a button ?
      By akomax
      Hey AI3 community ! I'm wondering how to do this :
      When I click a button on a GUI made (with koda), add a text somewhere in the GUI (most likely under the button) like, an "ok!" to say that no error happened <- If you can help on this too, I don't know how to check if any error occured after a Case, i know @error exist but I can't find how it works !
      And, I don't want an other GUI to appear, I want to do everyting in the same While Loop.
      Thanks to any helpers, hugs and kisses to your beautiful face