AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Easy CSV to Flat File conversion question...for geniuses.

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Originalsim



Joined: 08 Aug 2008
Posts: 9

PostPosted: Mon Aug 11, 2008 8:09 pm    Post subject: Easy CSV to Flat File conversion question...for geniuses. Reply with quote

Hello and thanks for your time in looking at this.
I have csv files that are created from the export command in adobe acrobat 8 pro. they look like this in notepad++ only but I haven't included the last 260 columns just the first 3.
Code:
,"form1[0].Associate[0]","form1[0].Chain[0]","form1[0].MID[0]"
Company X Application1.pdf,"123","555","12345678"


So, the associate is in the same column as 123 and the the chain is 555, etc.
Line 1, Column 1 is blank but line 2, column 1 contains the original filename.

Forgive me for being over explanatory, but image a blank notepad file and now divide it into a grid based on each spacebar hit being equal to 1 column and each carriage return being a row.

I have to get the csv data and output it to that blank text file BUT specific data has to go to specific rows and start in specific columns.
For example the data from the associate column (123) needs to be placed on line 2, 4 spaces out, the chain column data (555) has to be on line 2, starting 8 spaces out and the mid column data (12345678) has to be on line 3 starting 3 spaces out.
kinda like this.
Code:

   123 555
       12345678


What I have done is just tried to find the right functions and I figure all that I really need to do is use TXT_getCSV to define that data I need and TXT_ColPut to place that data where I need it. (Thanks heresy and HugoV for TXT.ahk)

My problem is that my CSV column names are not numbers which is stopping me from grabbing my data.
And I don't know how to use ColPut with data from getCSV and output it to my file.
Currently the Colput function will put the data on every line and I don't see how to stop it after it places my info where I need it.
Code:
TXT_ColPut(TextFile, StartColumn, Text, Skip = 0){ ; skip = shorter lines (e.g. lines shorter startcolumn position)
Original := A_BatchLines
SetBatchLines, -1
StartColumn--
    FileRead, Str, %TextFile%
TextFile := (SubStr(TextFile,1,1)="!") ? (SubStr(TextFile,2),OW=1) : TextFile
    Loop, Parse, Str, `n, `r
    {
   StringLeft, Section1, A_LoopField, StartColumn
        StringMid, Section2, A_LoopField, StartColumn
        If (Skip = 1) and (StrLen(A_LoopField) < StartColumn)
                     OutPut .= Section1 Section2 "`n"
            Else       
                     OutPut .= Section1 Text Section2 "`n"

    }
    If OW {
        FileDelete, %TextFile%
        FileAppend, %OutPut%, %TextFile%
    }
    Else {
        SplitPath, TextFile,, Dir, Ext, Name
        FileDelete, % Dir "\" Name "_copy." Ext
   FileAppend, %OutPut%, % Dir "\" Name "_copy." Ext
    }
SetBatchLines, %Original%
}



The main questions are (sorry for the long post):
Can I use getCSV to pull data from named columns.
How can I use that data to place into specific columns and then go to the next getcsv defined column.

I have about 2700 files that I need to do this to Smile So I am super excited to do it right the first time and just be able to run this on each file with hopefully only filename changes.

thanks in advance for any help.
Back to top
View user's profile Send private message
HugoV



Joined: 27 May 2007
Posts: 650

PostPosted: Mon Aug 11, 2008 9:46 pm    Post subject: Reply with quote

TXT_ColPut isn't going to be of much help here, it was intended to
insert a given text at a certain postion throughout a file.

Your solution would be to use Loop, parse, Inputvar, CSV (read help file) to parse the CSV file, store the CSV field in variable_a_Index and then reconstruct your output with the proper spacing and fileappend to a new file.
_________________
When parsing a CSV file use Loop, parse, Inputvar, CSV!
Back to top
View user's profile Send private message
Originalsim



Joined: 08 Aug 2008
Posts: 9

PostPosted: Tue Aug 12, 2008 5:18 pm    Post subject: Reply with quote

Thanks,
But my main problem is how to put the data into the specific spaces or columns of my flat text file.

Reading the colput function leaves me confused. I don't understand how stringleft and stringmid derive the startcolumn input point. I am pretty slow and I may be reading it incorrectly, also.

Thanks again.
Back to top
View user's profile Send private message
HugoV



Joined: 27 May 2007
Posts: 650

PostPosted: Tue Aug 12, 2008 7:21 pm    Post subject: Reply with quote

Forget about culput, you never heard of it, OK? You don't need it.
This should get you going:
Code:
InputVar="form1[0].Associate[0]","form1[0].Chain[0]","form1[0].MID[0]",Company X Application1.pdf,"123","555","12345678"
Loop, parse, InputVar, CSV
   CSV_%A_Index%:=A_LoopField
Outputvar=
(

  %CSV_2%
         %CSV_4% %CSV_1%
)   
FileAppend, %OutputVar%, testfile.txt
adjust spacing and fields to your needs.
_________________
When parsing a CSV file use Loop, parse, Inputvar, CSV!
Back to top
View user's profile Send private message
Originalsim



Joined: 08 Aug 2008
Posts: 9

PostPosted: Tue Aug 12, 2008 7:52 pm    Post subject: Reply with quote

Holy crap that's nearly perfect!

Am I doing something wrong, because when I run this it pulls the header names not the values. I realise that I can just change the csv_number, but I was curious if that was what you expected.

Do you know of any limits to ahk that would stop me from making one of my output values be in column 951 (its the furthest one out I have to place) of a regular document?

Thank you so much.
Back to top
View user's profile Send private message
Guest






PostPosted: Tue Aug 12, 2008 7:58 pm    Post subject: Reply with quote

No this is not possible
Back to top
Originalsim



Joined: 08 Aug 2008
Posts: 9

PostPosted: Tue Aug 12, 2008 8:12 pm    Post subject: Reply with quote

Are you saying that I cannot make my output be 951 spaces out?
Back to top
View user's profile Send private message
HugoV



Joined: 27 May 2007
Posts: 650

PostPosted: Tue Aug 12, 2008 8:18 pm    Post subject: Reply with quote

This will read your file into an array:
Code:
FileRead, Str, pathtofile
Loop, Parse, Str, `n, `r
{
   Row:=A_Index
   Loop, parse, A_LoopField, CSV
      {
      CSV_%Row%_%A_Index%:=A_LoopField
      }
      
}
Now simply create the outputvar for your FileAppend and I
don't see a reason why you should not be able to add as many spaces as
you would want.
_________________
When parsing a CSV file use Loop, parse, Inputvar, CSV!
Back to top
View user's profile Send private message
Originalsim



Joined: 08 Aug 2008
Posts: 9

PostPosted: Wed Aug 13, 2008 1:25 am    Post subject: Reply with quote

That works great and would probably fit the bill for 99% of everyone but not my needs, surprise surprise!

With this code, you have to know the lengths of the data that are going into each csv_x so that that the placing is correct.

placing something like
Code:
%csv_4%   %csv_5%
will only result in the csv_5 data being placed in 3 spaces after the end of csv_4 data regardless of how long or short the value of csv_4 is.

I am sure you don't want to hear it but the functionality you added to colput on InfoGulch's behalf is perfect for me. And I am heading there to ask a question.
Back to top
View user's profile Send private message
HugoV



Joined: 27 May 2007
Posts: 650

PostPosted: Wed Aug 13, 2008 7:24 am    Post subject: Reply with quote

Just use stringlength and add any missing spaces before adding it to outputvar, really you don't need colput, it is not meant for this purpose.
And if if you used it would be so much slower then any other solution
as it require multiple passes of the same file.
_________________
When parsing a CSV file use Loop, parse, Inputvar, CSV!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group