Jump to content

StringSplit end file .CSV


Recommended Posts

So if my file. Csv 'consists of 200 lines, for example, Excel spreadsheet, and 10 records per line (so I hypothetically 2000 information to manage) and I to be able to read the file and attach it to an array I have to make a loop if -then to find the end of the file, to associate the data mono dimensional array.

Then I do two concentric cycles to split the file you just extracted in a two-dimensional array (eg $ read [200] [10] as a maximum), in practice I had read the first 10 variables of the variable $ read [] and put them in the variable $ read1 [0] [0 ... 9], increasing the value of a unit size of the second row and read the information and riassiociarle 10 .... Right?.

$read=StringSplit(FileRead("ieri.csv"), ";")

Exsample file "ieri.csv" :

004100;000145;1;;;20120714

004100;000575;1;;;20120714

004100;001546;2;;;20120714

004100;001547;2;;;20120714

Program of test :

$read=StringSplit(FileRead("ieri.csv"), ";")

MsgBox(0,"read0",$read[0])

MsgBox(0,"read1",$read[1])

MsgBox(0,"read2",$read[2])

MsgBox(0,"read3",$read[3])

MsgBox(0,"read4",$read[4])

MsgBox(0,"read5",$read[5])

MsgBox(0,"read6",$read[6]) <--

MsgBox(0,"read7",$read[7]) <--

$A=($read[0]-1)/6

MsgBox(0,"$a",$A)

I have noticed that practically the variable $ read [6] contains both the value 20120714 of the first row is the value 004 100 of the second row (which would be the variable $ read [7]

In practice, the procedure works, but interprets the first line as the last field of the last real + the first of the next line.

I understand that the command interprets the lack of ";" at the end of the first line as a line not over until you reach the first ";" useful.

How can 'solve the problem, since all the files. Csv with which I will have to' work are missing a ";" at the end of the line?

Help Me !!

Thank you

Thank You

Alberto

---------------------------------------------------

I am translate with Google.

Link to comment
Share on other sites

To read a CSV file into a two dimensional array I use functions by ProgAndy.

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

$lines = StringSplit(FileRead("file.csv"), @CRLF, 1)
For $i = 1 To $lines[0]
    ConsoleWrite('line:' & $i & ' ')
    $columns = StringSplit($lines[$i], ";")
    For $j = 1 To $columns[0]
        ConsoleWrite('col' & $j & ':' & $columns[$j] & ' ')
    Next
    ConsoleWrite(@CRLF)
Next

Edited by Zedna
Link to comment
Share on other sites

  • 6 months later...

$lines = StringSplit(FileRead("file.csv"), @CRLF, 1)
For $i = 1 To $lines[0]
ConsoleWrite('line:' & $i & ' ')
$columns = StringSplit($lines[$i], ";")
For $j = 1 To $columns[0]
ConsoleWrite('col' & $j & ':' & $columns[$j] & ' ')
Next
ConsoleWrite(@CRLF)
Next

$Art = StringSplit(FileRead("C:\AutoIt\AutoIt\Prove\exp.txt"),@CRLF, 1)
For $f = 1 To $Art[0]
    ;ConsoleWrite('line:' & $f & ' ')
    $columns = StringSplit($Art[$f], ";")
    For $g = 1 To $columns[0]
      ; ConsoleWrite('col' & $g & ':' & $columns[$g] & ' ')
  $articoli[$f][$g] =$columns[$g]

    Next
  
Next

Hi, the routine that I had suggested to a month ago. CSV file now I've tried to use a file. TXT.

So okay to extrapolate the lines, but does not work to extrapolate the columns, or rather extrapolates only 2.

There are weeks that brooding but I could not figure out why and then ask for help to the creator of the routine.

I enclose a small part of the file for the actual is 90,000 rows.

It works the same way for both the original file and the part that I have attached.

Can you help me?

Thanksexp.txt

Edited by trescon

Thank You

Alberto

---------------------------------------------------

I am translate with Google.

Link to comment
Share on other sites

trescon,

When I run the data you posted through the code zedna gave you I get 8 lines of 134 columns each. What EXACTLY is the problem?

kylomas

The problem is that if I am going to check the variables $ art [0] have the right value of rows, I'll check if the variable $ columns [0] the value I get is 1 or 2 (can't remember now) instead of about 20/25 columns.

Why???

Thank you

Thank You

Alberto

---------------------------------------------------

I am translate with Google.

Link to comment
Share on other sites

Slightly modified version of Zedna's code. This should split the file you posted to a 2D array. As long as every line has the same number of elements to split this should work fine for you.

#include <array.au3>
#include <file.au3>
Local $Art
_FileReadToArray(@ScriptDir & "\exp.txt", $Art) ; reads the file and puts it into $Art
If Not @error Then
     $columns = StringSplit($Art[1], ";") ; find out how many columns there are
     Local $aArt[$Art[0]][$columns[0]] ; create a new array to hold the split file
     For $i = 1 To $Art[0]
          $columns = StringSplit($Art[$i], ";")
          For $j = 1 To $columns[0]
               $aArt[$i - 1][$j - 1] = $columns[$j] ; put the contents of each line into the array $aArt
          Next
     Next
     _ArrayDisplay($aArt) ; display the results, not needed for the function.
EndIf

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

Link to comment
Share on other sites

And this >>

So many options, you just have to search.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

Slightly modified version of Zedna's code. This should split the file you posted to a 2D array. As long as every line has the same number of elements to split this should work fine for you.

#include <array.au3>
#include <file.au3>
Local $Art
_FileReadToArray(@ScriptDir & "\exp.txt", $Art) ; reads the file and puts it into $Art
If Not @error Then
$columns = StringSplit($Art[1], ";") ; find out how many columns there are
Local $aArt[$Art[0]][$columns[0]] ; create a new array to hold the split file
For $i = 1 To $Art[0]
$columns = StringSplit($Art[$i], ";")
For $j = 1 To $columns[0]
$aArt[$i - 1][$j - 1] = $columns[$j] ; put the contents of each line into the array $aArt
Next
Next
_ArrayDisplay($aArt) ; display the results, not needed for the function.
EndIf

Hi, the program works well with the test file, if I put the actual files, which are about 70,000 rows I get the following error.

I should add that I have dimensioned the array $aArt [10000] [100]

The error does not change.

Why?

Thanks

C:AutoItAutoItProveGiacenzecafronto.au3 (11) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

$aArt[$i - 1][$j - 1] = $columns[$j]

^ ERROR

->07:22:45 AutoIT3.exe ended.rc:1

>Exit code: 1 Time: 12.330

Edited by trescon

Thank You

Alberto

---------------------------------------------------

I am translate with Google.

Link to comment
Share on other sites

My code works. But anyway, upload a sample files so we can test.

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

There was a warning posted in the message about the code I posted. If every line doesn't have the same amount of columns, then this won't work. It was an example of how it can be done, if you've got lines that have less splits, you're going to get that error. Also, I don't check for blank lines.

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

Link to comment
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
 Share

×
×
  • Create New...