stojko22

File read lines and add a comma after each line

3 posts in this topic

Hi,

 

I have a file with many rows that i want to convert to array.

The thing i need is to add a comma and 1 space after end of each line like "first line, second line, third line, fourth line".

Document looks like this

277
278
279
281
282
283
590
285
473
476
482
286
287
275
280
284
1059
1063
1067
1083
1071
1055

And here is code i try.

#include <File.au3>

$file = FileOpen("test.txt", 0)

$oFile = FileReadToArray($file)

For $i = 1 to UBound($oFile)-1

    $line = FileReadLine($file, $oFile[$i])
    $replace = $line & ", "

    ConsoleWrite($replace)

Next

 

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

stojko22,

Try something like this...

#include <File.au3>
#include <array.au3>

$file = FileOpen(@ScriptDir & "\test.txt", 0)
If $file = -1 Then Exit MsgBox(17, 'Error', 'Error opening file = ' & @ScriptDir & "\test.txt")

$aFile = FileReadToArray($file)
If @error Then Exit MsgBox(17, 'Error', 'File read error = ' & @error)

For $i = 0 To UBound($aFile) - 1

    $aFile[$i] = $aFile[$i] & ', '

Next

_ArrayDisplay($aFile)

File used for testing...test.txt

kylomas

edit: or simply...

#include <array.au3>

Local $new_file = StringRegExpReplace(FileRead(@ScriptDir & "\test.txt"), @CRLF, ', ' & @CRLF)

ConsoleWrite($new_file & @CRLF)

You have not said what you want the final result to be.

Edited by 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

Thanks alot @kylomas first example helped me.

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