Sign in to follow this  
Followers 0
trescon

StringSplit end file .CSV

13 posts in this topic

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.

Share this post


Link to post
Share on other sites



#3 ·  Posted (edited)

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

There's a few scripts for reading CSV in example scripts forum. Here's two.

And the one by Progandy already posted by Water.

Edited by czardas

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

$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

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

$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.

Share this post


Link to post
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


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

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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

And this >>

So many options, you just have to search.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

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.

Share this post


Link to post
Share on other sites

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


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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: 04/09/2015

Share this post


Link to post
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

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

    • DagSa
      By DagSa
      Try to find a solution for help files to my autoit executable.
      Its installed from simple XP-POS cash computers to desktop with windows 7, 8, 10.
      The compiled AutoIt program itself work everywhere, but the help file??
      I need a help file called from from the program and I don't really know what working best in all OS platforms.
      1. A wordfile is helpful enough, but word is not installed everywhere.
      2. RTF is very big files with pictures.
      3. I could save it as pdf, but not all have it either.
      4. Tried  .MHT file, but downside is browser could cover hole screen if browser closed this way. I have not found any way to resize it to specific size when open up.
      5. Use of IE.au3 lib. Well it could open mht files ,but always got question about use it as default browser or not. Think this also make IE browser lose control so I could not resize.
      6. Look at making a  .CHM file, but I don't think its the future do use that format. (.hlp is no no and old)

      Any tips of do it in autoit or make a call to open up in some nice format working everywhere.
       
    • au3leecher
      By au3leecher
      Hellow everybody,
      I tryed to manipulate a game called "Assault Cube" which is a easy beginning in coding. I run the code but the wished value doesn't change, i don't understand why.
      The Pointers are fine, I checked that the pointers don't change every time while restart the game.
      My Code:
      <snip>
      _____________________________________________________________________________________________________________________________________________________________________________________________
      Checklist:
      The Offsets are 18C & 4
      In Cheat Engine: ("ac_client.exe"+0010F418) <- Pointer
      4 Bytes = 'dword'
       
    • geekyworld
      By geekyworld
      Hi. I want to read the help file on my Kindle ebook reader (to protect my eyes). Is it available in Kindle or epub format? I tried converting it myself but the results were not very good.
      --Thanks!
    • JustinZandee
      By JustinZandee
      I need a random string generator which creates 15 letters/numbers.
      How can I make that?
    • StudioMaker
      By StudioMaker
      Hey anyone knows how i can have a inputbox that is connected to an HotKeySet? Like if u put in like "a" in the inputbox it sets the hotkey to "a"?