Sign in to follow this  
Followers 0

Parsing Text File


4 posts in this topic

Posted

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



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

Share this post


Link to post
Share on other sites

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

Posted

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

    • Delete String
      By asianqueen
    • AuPad
      By MikahS

      This is a notepad program written entirely in AutoIt. It has all the basics of notepad that you would expect. This is NOT feature complete, yet.

      I have included the source code for your pleasure, and feel free to give me pointers if you'd like to help out development (This is all done

      with AutoIt version 3.3.12.0) If you use any code from this, please leave a comment or something linking to this post, thank you.

      Prev dls from old forum: 783

      run the install to put the AuPad.exe and AuPad.ico in the program files directory under AuPad.

      Also, the install puts the RESH.au3 (all thanks to @Beege) UDFs into your include folder for you.

       

      Included in the .zip: Install.exe, Install.au3, AuPad.au3, AuPad.exe,

      aupad.ico, uninstall.au3, Uninstall.exe, and RESH.au3

      Current Features:

       

      File - New, Open, Save, Save As, Exit, Print

      Edit - Undo, Cut, Copy, Paste, Del, Find, Replace, Select All, Tab, Time/Date, Word Count, Line Count, Uppercase Text, Lowercase Text

      Format - Word Wrap, Font, Font Color, Background Color

      Help - About

      ---Rich Edit (picture support), Au3 Syntax Highlighting, Drag & Drop support, open any file type, Grey certain menu items if there is txt or sel, print by line support---

      Compile scripts, Forum Tags, version history, web search, au3 helpfile search, text to speech, recent files, Syntax highlighting for au3 when opened

      Custom Context Menu for Rich Edit

       

      Version History:

      ---==== 1.0.0 ====---

      - Basic file features (open, new, save, save as, print, exit, )

      - Basic edit features (undo, cut, copy, paste, find, replace, delete, select all, time/date)

      - Basic format features (word wrap, font)

      - Basic help features (help, about)

       

      ---==== 1.5.0 ====---

      - Took out extra save dialog in the Save() function.

      - Drag and drop functionality added (thanks to AZJIO)

      - Took out check for txt file when opening

      - Fixed open function to delete text already in control and ask to save

      - Added $ws_ex_composite to stop flicker on xp machines.

      - Added default font

      - Set the font in the setWW() function

      - added word count functionality with a accelerator key

       

      ---==== 1.5.1 ====---

      - Fixed word count function

      - Added line count thanks to DreamVB

      - Added uppercase and lowercase thanks to DreamVB

       

      ---==== 1.6.0 ====---

      - Added better text limit

      - Fixed the edit control to show the horizontal scrollbar

      - Made resizing of the edit control AUTO; making the menu AUTO, instead of just 5 pixel height

      - Changed any file over 100 MB to read in as binary

      - Changed any file dragged and dropped to read in as binary if over 100 MB in size

      - Added accelerator keys for uppercase, lowercase and save as

      - Took out unnecarry MsgBox in save function

      - Added print by line support

      - Added ini file for settings saved

      - Added setting in ini for word wrap

       

      ---==== 1.7.0 ====---

      - Added cancel button when quitting

      - Took out extra include

      - Set default font into font variables

      - Set default font when word wrap has been selected

      - Set default font when taking off word wrap

      - Added RTF files and All files to save dialog

      - Added redo functionality

      - added character attribute functionality for rich edit

      - added rtf files to the open file dialog

      - Added color support

      - Added picture support

      - Added background color support

      - Added stream rtf from file support

      - Added stream rtf to file support

      - Added recent files to menu item

      - Added AutoIt syntax highlighting; thanks goes to Beege for RESH UDF

      - Took out word wrap function and menu item as RichEdit is word wrapped already

      - Took out ini for the word wrap, but want to be very portable anyway

      - Added check for character attributes change

      - Added au3 file type for open and save dialog boxes

       

      ---==== 1.8.0 ====---

      - Fixed upper, lower, tab

      - Fixed resizing of window and rich edit control

      - Fixed recent files

      - Fixed time/date at cursor position

      - Fixed RESH.au3 comment block functionality (added check in Do..While loop & added -1 to Ubound call)

       

      ---==== 1.8.2 ====---

      - Took out HotkeySets and made GUI accelerator keys (F2 (about), F5 (time/date))

      - Fixed Au3 syntax highlighting setting caret position

      - Added cancel to New file dialog

      - Switched to ASM RESH UDF by Beege (thanks!)

      - Added check when Syntax Highlighting to not highlight if there has not been any more user interaction. Cutting down a ton of processing.

      - Added check for quotes, due to problem with RESH - It will crash the entire program when trying to syntax highlight an unterminated quote.

      - Commented out Addrecent as problems with messageloop and GUI dummy

      - F2 was fixed to bring up help instead of about AuPad.

       

      ---==== 1.8.4 ====---

      - added version developed in the about child window

      - added tags quote, link, autoit (code). (all thanks for the Idea from The Saint's webpad -

      - added GUI Accelerator keys for tags.

      - added compiler GUI.

      - added ability to compile au3 scripts.

      - ability to open aut2exe for more options.

       

      ---==== 1.8.6 ====---

      - fixed tag labels to show gui accelerator keys.

      - fixed add recent function.

      - ability to open recent files in recent files menu.

      - Added text to speech ability (idea from betapad - http://www.autoitscript.com/forum/topic/38353-betapad/

      - added menu item for text to speech

      - accel key added for text to speech and web search

      - added child gui for web search

      - fixed tag accelerator keys to ctrl + shft combo.

      - added ability to search web using 4 search providers in seperate gui.

      - added accelerator keys for web search GUI.

      - fixed version number in directives.

      - added version history gui.

       

      ---==== 1.8.8 ====---

      - took off $ES_READONLY style on version history edit control.

      - fixed view about aupad event. When opening it would call folderpath() in a loop, making it unable to close.

       

      ---==== 1.9.0 ====---

      - added ability to highlight, then websearch the selected text.

      - Took out printing UDF by martin

      - Took out x86 force directive. Can now be used as x64.

      - ability to open AutoIt helpfile.

      - ability to highlight and search the AutoIt helpfile with keyword.

      - changed _guictrlrichedit_setsel() hide sel to false, instead of true, as it would hide the selection after changing the highlighting.

      - printing will print to default printer only. Trying to make more portable, which will exclude print dll and library now. Changing of printer is in the works.

      - took out debugging code from last version to fix folderpath() loop.

       

      ---==== 1.9.1 ====---

      - added default style to web search button. - idea by mLipok.

      - deleted the child window for web search on successfully opening IE.

      - added abillity to open au3 file and have syntax highlighting start automatically. - idea by mLipok.

      - added tooltip to show when syntax highlighting for AutoIt has started or ended.

       

      ---==== 1.9.2 ====---
      - added default style to version history "okay" button (un-highlights edit text).
      - added dpi awareness to about AuPad GUI. Thanks to mLipok -http://www.autoitscript.com/forum/topic/166479-writing-dpi-awareness-app-workaround/
      - added context menu with much thanks to Mat -
      - state changing for context menu, just like regular menu items.
      - events with context menu use GUI accelerators.
    • Get selected text?
      By CaptainSparky
  • Recently Browsing   0 members

    No registered users viewing this page.