IanN1990

_FileReadToArray issue [Solved]

10 posts in this topic

#1 ·  Posted (edited)

Hi, 

I am trying to compare the data within two .csv files but am running into an issue. 

#include "file.au3"
Local $ArrayA

_FileReadToArray("c:\test.CSV", $ArrayA, 0, ",")
_ArrayDisplay($ArrayA)

This is returning an error code of 3 - File lines have different numbers of fields (only if $FRTA_INTARRAYS flag not set).

Example Data

ID                 Access Granted
TESTING      Mar 31, 2016 2:03:31 PM
TESTING      Mar 1, 2017 10:31:00 AM

 

I believe this is caused because the Access Granted is a date containing a , which is also the condition for the split. How would i get around this?

Edited by IanN1990

Share this post


Link to post
Share on other sites



#2 ·  Posted

Why do you need to split the lines? Can't you just do it without doing the stringsplit?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#3 ·  Posted

IanN1990,

The date is NOT the cause.  Some record(s) have different number of fields (commas).

Kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#4 ·  Posted

3 hours ago, BrewManNH said:

Why do you need to split the lines? Can't you just do it without doing the stringsplit?

Though my example was limited, the full .csv file is 10,000 entries and 7 columns wide. Some data does repeats between columns.

1 hour ago, kylomas said:

IanN1990,

The date is NOT the cause.  Some record(s) have different number of fields (commas).

Kylomas

From my troubleshooting i cant see what else it can be. Below is a improved example illustrating the problem
 

#include "file.au3"
#NoTrayIcon
Local $ArrayA
Local $ArrayB

_FileReadToArray("C:\Working.txt", $ArrayA, 0, ",")
ConsoleWrite(@error & @crlf)
_ArrayDisplay($ArrayA)

_FileReadToArray("C:\NotWorking.txt", $ArrayB, 0, ",")
ConsoleWrite(@error & @crlf)
_ArrayDisplay($ArrayB)

Working.txt - (Edited .csv saved as txt removing commas from date)

ID,Name,Email,Username,DB,Access Granted,Change
0,Test Test,test.test@test.com,test_test,test,Mar 31 2016 2:03:31 PM,TESTING
0,Test Test,test.test@test.com,test_test,test,Mar 1 2017 10:31:00 AM,TESTING

Notworking.txt - (Original unedited .csv saved as .txt)

ID,Name,Email,Username,DB,Access Granted,Change
0,Test Test,test.test@test.com,test_test,test,"Mar 31, 2016 2:03:31 PM",TESTING
0,Test Test,test.test@test.com,test_test,test,"Mar 1, 2017 10:31:00 AM",TESTING

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

InaN1990,

Are you sure that you don't have a blank line at the start or end?

Can you post the file that you are using?

kylomas

edit: You might try a simple script to run through each line and counts the commas per line.  Stringreplace give you a count of the number of replacements.

Edited by kylomas
additional data
1 person likes this

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Files uploaded as requested.

Funny enough i had the same idea and am editing _FileReadToArray to do just that.

NotWorking.txt

Working.txt

Edited by IanN1990

Share this post


Link to post
Share on other sites

#7 ·  Posted

If you have Excel you could just change the extension to .csv and it will create the array correctly.

#include <Array.au3>
#include <Excel.au3>
Local $sFilePath = @ScriptDir & "\NotWorking.csv"
Local $oExcel = _Excel_Open(False)
Local $oWorkBook = _Excel_BookOpen($oExcel, $sFilePath)
Local $aWorkBook = _Excel_RangeRead($oWorkBook)
_Excel_BookClose($oWorkBook)
_Excel_Close($oExcel)
_ArrayDisplay($aWorkBook, "Before")
For $i = 0 To UBound($aWorkBook) - 1
    $aWorkBook[$i][5] = StringReplace($aWorkBook[$i][5], ",", "")
Next
_ArrayDisplay($aWorkBook, "After")

 

1 person likes this

Share this post


Link to post
Share on other sites

#8 ·  Posted

As explained in the help file for _FileReadToArray, the stringsplit algorithm in the function is limited. Use one of the many CSV functions that you can find in the Examples forum. For example this one;

 

1 person likes this

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

7 hours ago, Subz said:

If you have Excel you could just change the extension to .csv and it will create the array correctly.

The code provided works but I have found Excel com objects very unreliable on my system.

7 hours ago, BrewManNH said:

As explained in the help file for _FileReadToArray, the stringsplit algorithm in the function is limited. Use one of the many CSV functions that you can find in the Examples forum. For example this one;

CSVSplit was the answer :) Thank you BrewManNH

Edited by IanN1990

Share this post


Link to post
Share on other sites

#10 ·  Posted

:thumbsup:


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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