Sign in to follow this  
Followers 0
EmptySpace

Replace empty space with comma in txt file

7 posts in this topic

Hey, I need some help from you guys.

For example my text

text just text wait wut
after script work:
text,just,text,wait,wut

Maybe I need to use StringRegExp()? I never used it before.

So ok my code:

#include
Global $pw = FileOpen("textwithspaces.txt")
Global $hOutPutFile = FileOpen("nospaces.txt", $FO_OVERWRITE)

$hash = FileRead($pw)

;I need array loop or something?
;I would try to read line by line and use stringinstr or smth but I beleive there is faster way

WEnd
FileClose($pw)
FileClose($hOutPutFile)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

stringRegExpReplace ($hash,chr(32),",")

or just stringreplace

Edited by jdelaney

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

#3 ·  Posted (edited)

Easy task ;)

#include <File.au3>

$sData = "text just text wait wut"
$FileName = "Test.txt"

$hFile = FileOpen($FileName, 2)
FileWrite($hFile, $sData)
FileClose($hFile)

$hFileOpen = FileOpen($FileName)
$ReadData = FileRead($hFileOpen)
FileClose($hFileOpen)
$sNewFile = StringReplace($ReadData, chr(32), ',')
$hFileOpen = FileOpen($FileName, 2)
FileWrite($hFileOpen, $sNewFile)
FileClose($hFileOpen)

ShellExecute('test.txt')
Edited by MyEarth

Share this post


Link to post
Share on other sites

Thanks! :thumbsup:

Share this post


Link to post
Share on other sites

EdgarT,

In case there are multiple spaces between the tokens, a comma already present or you want to span multiple lines.

#include <Constants.au3>

#AutoIt3Wrapper_Add_Constants=n

;===================================================================================
; create test file
;===================================================================================

$str = 'token1     token2 token3 token4      token5' & @crlf & _
       'token6,  token7,token8' & @crlf & _
       'token9' & @crlf & _
       'token10,token11      token12'

local $hfl = fileopen (@scriptdir & '\test.txt',2)
filewrite ($hfl,$str)
fileclose($hfl)

;===================================================================================
; read file and replace all spaces between words with a ',', if one does not exist
;===================================================================================

msgbox($mb_ok,'Text Before',fileread(@scriptdir & '\test.txt'))
msgbox($mb_ok,'Text After ',stringregexpreplace(fileread(@scriptdir & '\test.txt'),'\b(\x20)+\b',','))

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

#6 ·  Posted (edited)

good call...or:

$string = stringRegExpReplace ("text just text wait wut", chr(32)&"+",",")

although, yours would be good if you don't want to replace leading/trailing spaces

Edited by jdelaney

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

And to avoid double commas

$string = stringRegExpReplace ("text, just text, wait wut", '[\h,]+', ",")
msgbox(0,"", $string)

:idiot:

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