Sign in to follow this  
Followers 0
maqleod

Find number of lines in text

7 posts in this topic

Hey all, I'm writing a POI editor for GPS units. What I am having problems figuring out is how to pull the data from a .csv file and put it to a list view.

The .csv file will be formatted as follows:

latitude,longitude,name,address city state zip

each line will be displayed in the listview the same way, line by line.

I guess I just need suggestions on the best way to pull data from the .csv file to be able to keep the lines easily intact for formatting into the listview. I will also want the ability to edit fields in the list view and have that reflected in the .csv file and the ability to delete a line altogether. I'm thinking filecountlines() filereadline() stringsplit()...for the reading part, using filewritetoline() for the editing part, unless someone can suggest a better way for these, but I'm not sure how to delete a single line in the .csv. I can equate the field number in the listview to the line number, but how to I go about deleting that line in the file? filewritetoline() but use blank entries? I'd also prefer to keep the file legible for reading in a text editor...


[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites



Hey all, I'm writing a POI editor for GPS units. What I am having problems figuring out is how to pull the data from a .csv file and put it to a list view.

The .csv file will be formatted as follows:

latitude,longitude,name,address city state zip

IMHO the easiest way for you is to read that CSV file to an array. You can easily process the lines and the line count is found in array[0]

#include <array.au3>; for _arraydisplay() only
#include <file.au3>   ; for _FileReadToArray() and _FileWriteFromArray()

Dim $MyCSVfileIn="C:\path\path\myGPS.csv"
Dim $MyCSVfileOut="C:\path\path\myGPSModified.csv"
Dim $MyCSVArr[1]

_FileReadToArray($MyCSVfileIn,$MyCSVArr)
_ArrayDisplay($MyCSVArr,"Number of lines is to be found in [0]")

; do your modifications here

_FileWriteFromArray($MyCSVfileOut,$MyCSVArr)

Regards, Rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

Can you post a sample .csv file that would be used?

Depending on the layout of the csv file I'd use FileRead and stringsplit on the ending of a line (@CRLF).

Then the return stringsplit gives at [0] the count.

Roughly something like:

$csv = @ScriptDir & "\file.csv"
$aCsv = StringSplit(StringStripWS(StringStripCR(FileRead($csv)), 3), @LF)
ConsoleWrite("Line Count of CSV: " & $aCsv[0] & @LF)

Seeing the exact layout of the csv file would give to better insight of how to handle the read and write of it from your listview.

Cheers

Share this post


Link to post
Share on other sites

Can you post a sample .csv file that would be used?

Depending on the layout of the csv file I'd use FileRead and stringsplit on the ending of a line (@CRLF).

[...] ; doesn't this also just give an array like _filereadtoarray() :)

Seeing the exact layout of the csv file would give to better insight of how to handle the read and write of it from your listview.

As initially posted, the file structure for POIs is this:

The .csv file will be formatted as follows:

latitude,longitude,name,address city state zip

One line per POI (Point of interest)

regards, rudi.


Earth is flat, pigs can fly, and Nuclear Power is SAFE!

Share this post


Link to post
Share on other sites

As initially posted, the file structure for POIs is this:

One line per POI (Point of interest)

regards, rudi.

Yes I'm aware of this, but what's to say there's not other data in the file like empty lines, comments or any misc. text that needs to be filtered or vice versa.

Cheers

Share this post


Link to post
Share on other sites

Yes I'm aware of this, but what's to say there's not other data in the file like empty lines, comments or any misc. text that needs to be filtered or vice versa.

Cheers

Well, I guess there is always the chance that the POI files might have comments or other things, but the ones I've downloaded as samples usually all follow what I explained, but to specify further, no empty lines or comments or other text explaining anything that needs filtering. One set contained a commented line for the first line explaining the structure. I will eventually allow the creation of a POI from the script that will not comment, so for now I'm just going to assume that the typical file someone will open will not contain anything but the strict format.

This is my code so far (I didn't go with the array suggestion as of yet, still think this is simpler, but I may find reasons later to alter it - still need a way to cleanly delete a specified line in the file though):

#include <GUIConstants.au3>
#include <GuiListView.au3>
#include <File.au3>
#NoTrayIcon

$name = "POI Editor"

$fileopen = 0

$parent = GUICreate($name,455,550,-1,-1,-1, $WS_EX_ACCEPTFILES)
Opt("RunErrorsFatal", 0)
$contextmenu = GUICtrlCreateContextMenu()
$fileopt = GUICtrlCreateMenu ("&File")
$exititem = GUICtrlCreateMenuitem ("Exit",$fileopt)
$helpopt = GUICtrlCreateMenu ("About")
$aboutitem = GUICtrlCreateMenuitem ("About",$helpopt)

GuiCtrlCreateLabel("File:",10,10,75,25)
$target = GUICtrlCreateInput("",35,5,325,20)
GuiCtrlSetState($target,$GUI_DISABLE)
$open = GUICtrlCreateButton ("Open",370,5,75,25)

$add = GuiCtrlCreateLabel("Add New Entry",10,50,75,25)
$latitude = GuiCtrlCreateLabel("Latitude:",10,70,75,25)
$longitude = GuiCtrlCreateLabel("Longitude:",140,70,75,25)
$placename = GuiCtrlCreateLabel("Name:",270,70,75,25)
$address = GuiCtrlCreateLabel("Address:",10,120,75,25)
$city = GuiCtrlCreateLabel("City:",170,120,75,25)
$state = GuiCtrlCreateLabel("State:",300,120,60,25)
$zip = GuiCtrlCreateLabel("Zip:",370,120,60,25)

$latdata = GUICtrlCreateInput("",10,90,125,20)
$longdata = GUICtrlCreateInput("",140,90,125,20)
$namedata = GUICtrlCreateInput("",270,90,175,20)
$adddata = GUICtrlCreateInput("",10,140,155,20)
$citydata = GUICtrlCreateInput("",170,140,125,20)
$statedata = GUICtrlCreateInput("",300,140,65,20)
$zipdata = GUICtrlCreateInput("",370,140,75,20)

$clearentry = GuiCtrlCreateButton("Clear",290,170,75,25)
$addentry = GuiCtrlCreateButton("Add",370,170,75,25)

$existing = GuiCtrlCreateLabel("Edit/Delete Existing Entries",10,210,175,25)
$listview = GuiCtrlCreateListView("Latitude          |Longitude          |Name               |Address                    ",10,230,435,240)

$editentry = GuiCtrlCreateButton("Edit",10,480,75,25)
$deleteentry = GuiCtrlCreateButton("Delete",90,480,75,25)

$about = GUICtrlCreateButton ("?",340,500,25,25)
$close = GUICtrlCreateButton ("Close",370,500,75,25)

GUISetState()
Do
$msg = GUIGetMsg()

if $msg = $open then
$csvfile = FileOpenDialog("Open POI","C:\","Comma Separated Value (*.csv)")
GuiCtrlSetData($target,$csvfile)
FileOpen($csvfile,1)
$fileopen = 1
_GUICtrlListViewDeleteAllItems($listview)
$filelines = _FileCountLines($csvfile)
For $i = 1 to $filelines
$data = FileReadLine($csvfile,$i)
$string = StringSplit($data,",")
GuiCtrlCreateListViewItem($string[1] & "|"& $string[2] & "|"& $string[3] & "|"& $string[4],$listview)
Next
endif

if $msg = $clearentry then
GuiCtrlSetData($latdata,"")
GuiCtrlSetData($longdata,"")
GuiCtrlSetData($namedata,"")
GuiCtrlSetData($adddata,"")
GuiCtrlSetData($citydata,"")
GuiCtrlSetData($statedata,"")
GuiCtrlSetData($zipdata,"")
endif
if $msg = $addentry then
if $fileopen = 1 then
FileWriteLine($csvfile,GuiCtrlRead($latdata) & "," & GuiCtrlRead($longdata) & "," & GuiCtrlRead($namedata) & "," & GuiCtrlRead($adddata) & " " & GuiCtrlRead($citydata) & " " & GuiCtrlRead($statedata) & " " & GuiCtrlRead($zipdata))
GuiCtrlCreateListViewItem(GuiCtrlRead($latdata) & "|" & GuiCtrlRead($longdata) & "|" & GuiCtrlRead($namedata) & "|" & GuiCtrlRead($adddata) & " " & GuiCtrlRead($citydata) & " " & GuiCtrlRead($statedata) & " " & GuiCtrlRead($zipdata),$listview)
GuiCtrlSetData($latdata,"")
GuiCtrlSetData($longdata,"")
GuiCtrlSetData($namedata,"")
GuiCtrlSetData($adddata,"")
GuiCtrlSetData($citydata,"")
GuiCtrlSetData($statedata,"")
GuiCtrlSetData($zipdata,"")
else
MsgBox(64,"Error","Please open a file first   ")
endif
endif

if $msg = $editentry then
if $fileopen = 1 then
$index = _GUICtrlListViewGetCurSel($listview)
if $index = $LV_ERR then
MsgBox(64,"Error","Please select an entry to edit   ")
else
$child2 = GUICreate("Edit Entry",455,180,-1,-1,-1,$WS_EX_ACCEPTFILES,$parent)
$latitude1 = GuiCtrlCreateLabel("Latitude:",10,10,75,25)
$longitude1 = GuiCtrlCreateLabel("Longitude:",140,10,75,25)
$placename1 = GuiCtrlCreateLabel("Name:",270,10,75,25)
$address1 = GuiCtrlCreateLabel("Address:",10,60,75,25)
$city1 = GuiCtrlCreateLabel("City:",170,60,75,25)
$state1 = GuiCtrlCreateLabel("State:",300,60,60,25)
$zip1 = GuiCtrlCreateLabel("Zip:",370,60,60,25)

$datum = StringSplit(_GUICtrlListViewGetItemText($listview,$index,3)," ")
$latdata1 = GUICtrlCreateInput(_GUICtrlListViewGetItemText($listview,$index,0),10,30,125,20)
$longdata1 = GUICtrlCreateInput(_GUICtrlListViewGetItemText($listview,$index,1),140,30,125,20)
$namedata1 = GUICtrlCreateInput(_GUICtrlListViewGetItemText($listview,$index,2),270,30,175,20)
$adddata1 = GUICtrlCreateInput($datum[1],10,80,155,20)
$citydata1 = GUICtrlCreateInput($datum[2],170,80,125,20)
$statedata1 = GUICtrlCreateInput($datum[3],300,80,65,20)
$zipdata1 = GUICtrlCreateInput($datum[4],370,80,75,20)

$ok = GUICtrlCreateButton ("OK",150,140,75,25)
$cancel = GUICtrlCreateButton ("Cancel",235,140,75,25)
GUISetState()
Do
$msg2 = GUIGetMsg()
if $msg2 = $ok then
_GUICtrlListViewSetItemText($listview,$index,0,GuiCtrlRead($latdata1))
_GUICtrlListViewSetItemText($listview,$index,1,GuiCtrlRead($longdata1))
_GUICtrlListViewSetItemText($listview,$index,2,GuiCtrlRead($namedata1))
_GUICtrlListViewSetItemText($listview,$index,3,GuiCtrlRead($adddata1) & " " & GuiCtrlRead($citydata1) & " " &GuiCtrlRead($statedata1) & " " &GuiCtrlRead($zipdata1))
_FileWriteToLine($csvfile,($index + 1),GuiCtrlRead($latdata1) & "," & GuiCtrlRead($longdata1) & "," & GuiCtrlRead($namedata1) & "," & GuiCtrlRead($adddata1) & " " & GuiCtrlRead($citydata1) & " " & GuiCtrlRead($statedata1) & " " & GuiCtrlRead($zipdata1),1)
ExitLoop
endif
if $msg2 = $cancel then
ExitLoop
endif
Until $msg2 = $GUI_EVENT_CLOSE
GUIDelete($child2)
endif
else
MsgBox(64,"Error","Please open a file first   ")
endif
endif

if $msg = $deleteentry then

endif

if $msg = $aboutitem or $msg = $about then
$child1 = GUICreate("About",220, 220,-1,-1,-1,$WS_EX_ACCEPTFILES,$parent)
$font = "Ariel"
Opt("RunErrorsFatal", 0)
GUICtrlCreateLabel ($name, 50, 30, 150,30)
GUICtrlSetFont (-1,10, 400, $font)
GUICtrlCreateIcon("icons\POI.ico",-1,85,60,48,48)
GUICtrlCreateLabel ("Written By MaQleod",  55, 120, 150,30)
GUICtrlSetFont (-1,10, 400, $font)
$aboutok = GUICtrlCreateButton ("OK", 75, 170, 75,25)
GUISetState()
Do
$msg1 = GUIGetMsg()
if $msg1 = $aboutok then
ExitLoop
endif
Until $msg1 = $GUI_EVENT_CLOSE
GUIDelete($child1)
endif

if $msg = $close or $msg = $exititem then
FileClose($csvfile)
ExitLoop
endif

Until $msg = $GUI_EVENT_CLOSE
GUIDelete()

[u]You can download my projects at:[/u] Pulsar Software

Share this post


Link to post
Share on other sites

I changed the delete entry code to this:

if $msg = $deleteentry then
if $fileopen = 1 then
$index = _GUICtrlListViewGetCurSel($listview)
if $index = $LV_ERR then
MsgBox(64,"Error","Please select an entry to delete   ")
else
;$deletedline = _FileDeleteLine($csvfile,($index + 1))
_FileWriteToLine($csvfile,($index + 1),"",1)
_GUICtrlListViewDeleteItem($listview,$index)
endif
else
MsgBox(64,"Error","Please open a file first   ")
endif
endif

after reading a while on the forums I found that filewritetoline() used with "" will delete the line entirely and leave the file in a clean manner for further reading and editing.


[u]You can download my projects at:[/u] Pulsar Software

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