Sign in to follow this  
Followers 0
prmanningham

CSV parsing

14 posts in this topic

Hi guys, I've tried a few UDFs to help me achieve my goal but I unfortunately didn't suceed yet.

I don't know if one of you has an easy solution for the goal I'm trying to achieve.

Here's what I'm trying to do :

I have a csv of 1191 lines with 3 values on each lines separated by ";"

I would like to separate each value and process each line in a loop.

Thanks in advance for the help.

Share this post


Link to post
Share on other sites



We're going to need more information than that. what do you mean when you say "I would like to separate each value and process each line in a loop."? Explain what it is you're trying to do with each line, what you're trying to do with each value that you're separating in the line. I'd start by looking at FileRead, or _FileReadToArray, or FileReadline and then use StringSplit on the lines of the file.


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

Sorry for not providing enough details.

First value is workstation name, second is the path to file I'm trying to process and third is the username

The script needs to check if local workstation name = value1. If positive copy value2 to a network share while renaming the file to value3-%time%.pst and so on for each line.

Thanks for pointing me to StringSplit. This seems to be what I was looking for :)

Share this post


Link to post
Share on other sites

This will work:

#include <Array.au3>
$string = "test1,test2,test3" & @crlf & "test4,test5,test6" & @CRLF & "test7,test8,test9"
$atest = StringRegExp( $string, "(.*),(.*),(.*)", 4)
_ArrayDisplay ( $atest )
For $i=0 To UBound ( $atest ) - 1
 _ArrayDisplay ( $atest[$i] )
Next

replace the string with the file's text


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

To read/write a CSV file to/from an array you could use this UDF.


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

#6 ·  Posted (edited)

Water, the link seems to be broken.

prmanningham, I have been using my UDF for a while now without ever experiencing problems. ProgAndy has also written a you might like to try. There are one or two small differences.

Edited by czardas

Share this post


Link to post
Share on other sites

I checked the link. one works.


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

thanks all!

I tried this and I keep getting an error saying : "Array variable has incorrect number of subscripts or subscript dimension range exceeded."

Shoud this be displaying each line of my CSV?

#include <array.au3>
#include <CSV.au3>

$rc = _ParseCSV("c:testpst.csv", ";")

For $i = 0 To UBound($rc)
    _ArrayDisplay($rc[$i])
    ; You can now use the array as necessary
Next

Share this post


Link to post
Share on other sites

Try

_ArrayDisplay($rc)
_ArrayDisplay is able to display 1D and 2D arrays.


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

You would only need to loop through the array if it were an array of arrays, like in my example. Yours is considered a 2D array.

Also, you should use the upper limit as UBound()-1, when it is a legit loop you need to do.


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

EDITED OUT (oops I just saw what the problem was - answered by water)

jdelaney, I don't think using an array of arrays is more suitable. It is recommended that you avoid doing this.

prmanningham, you need to subtract 1 if you use ubound to loop through the rows.

For $i = 0 To UBound($rc) -1 ; Subtract 1

If you want to loop through the columns, you need a second loop inside the first loop. No time to demonstrate right now, but someone will help you further, I'm sure. :)

Edited by czardas

Share this post


Link to post
Share on other sites

It's an autoit function that produces it :P

I see. You're forgiven. :rolleyes:
1 person likes this

Share this post


Link to post
Share on other sites

Awesome guys, I've been able to finish my script with your help!

Thanks a lot!

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