Jump to content
Sign in to follow this  
trescon

StringSplit end file .CSV

Recommended Posts

trescon

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
water

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
czardas

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
Zedna

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

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

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

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
BrewManNH

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
guinness

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

Share this post


Link to post
Share on other sites
trescon

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
guinness

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

Share this post


Link to post
Share on other sites
BrewManNH

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  

  • Similar Content

    • chacoya121
      By chacoya121
      can someone plz explain how WinAPI work and some example script plz
    • watchoverme
      By watchoverme
      hi all, how can i move mouse to the place where pixel changes
       
      While Sleep (3000)
      $pix = PixelChecksum(0,0,55,55)
      If IsArray($pix) = True Then
      MouseMove($pix [0],$pix[1])
      EndIf
      WEnd
    • Ignacio
      By Ignacio
      Hello, and good day
      Im trying to make a sentence autocompleter so that when you type certain words (or commands) the scripts, and im in need of help/pointers
      That is what i have at the time and the issues i have currently is that:
      - I cant find an easy way to reset the counter to 0 in case a different letter from those are pressed ( tried NOT _ispressed but i think i got it wrong)
      -Is there another way to detect the key press that _ispressed? (i couldnt find it so far), since i feel like it is too clunky ( although maybe that is just me and my way to code)
      - for some reason the hex code (6F) for the / (divide nume pad) isnt working for me
       
      Im thinking of making a text file with some words to use them as variables/comparations  (so that at least removes the need of a variable for the words in the script) and make the script make a temporal text file to save the input and then compare it with the other one. But i dont know if that is a good approach.
      Thanks for your time and patience.
    • WiorDi37
      By WiorDi37
      Hello, Everyone!
      I want when clicking the exit button the window will close. If content changes upon exit the program will automatically choose not save.
      Look forward to the help, thanks.
      #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <AutoItConstants.au3> $GUI = GUICreate("Form1", 220, 119, 192, 124, $WS_SYSMENU) GUISetFont(10, 400, 0, "Tahoma") GUICtrlCreateGroup("Chuẩn bị trình chiếu", 16, 16, 185, 65) $ok_Button = GUICtrlCreateButton("Ok", 32, 48, 75, 25) $exit_Button = GUICtrlCreateButton("Exit", 112, 48, 75, 25) GUICtrlCreateGroup("", -99, -99, 1, 1) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $ok_Button ShellExecute(@MyDocumentsDir&'\Dich-thuat\Short-Document.pdf', "", "", Default, @SW_MAXIMIZE) WinWaitActive("Data and Computer Communications (Eighth Edition) - Google Chrome") ShellExecute(@MyDocumentsDir&'\Dich-thuat\Document.rtf', "", "", Default, @SW_MAXIMIZE) WinWaitActive("Document.rtf [Compatibility Mode] - Word") ShellExecute(@MyDocumentsDir&'\Dich-thuat\Presentation1.pptx', "", "", Default, @SW_MAXIMIZE) WinWaitActive("Presentation1.pptx - PowerPoint") MouseClick("left", 1381, 886, 1) Sleep(2000) MsgBox(64, "Thông báo", "Đã chuẩn bị xong") Case $exit_Button WinClose("Presentation1.pptx - PowerPoint") ;I need help handling this place Case $GUI_EVENT_CLOSE Exit EndSwitch WEnd  

    • badcoder123
      By badcoder123
      Hey everyone,
      I've been messing around with some new things and adlibs look extremely useful/interesting, however, I can't seem to get it to work.. Here's what I have
      HotKeySet("{F1}", "_Exit") $qCount = 1 Global $_Timer AdlibRegister($_Timer, 1000) AdlibUnRegister($_Timer) While 1 SoundPlay(@WindowsDir & "\media\tada.wav", 1) $qCount += 1 ToolTip('"Tada" has been played ' & $qCount & " times",200,200) WEnd Func _Timer() Local Static $iCount += 1 ConsoleWrite($iCount) If $iCount = 20 Then ConsoleWrite("iCount is at " & $iCount) EndFunc Func _Exit() Exit EndFunc Basically I want it to call the _Timer function but it doesn't seem to work :/ any ideas?
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.