Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Replacing multiple characters in string

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
mmharris
246 Views, 2 Replies

Replacing multiple characters in string


#input file
fin = open("H:/AFILES/Test2.txt", "rt")
#output file to write the result to
fout = open("H:/AFILES/Test2Replaced.txt", "wt")
#for each line in the input file
for line in fin:
#read replace the string and write to output file
fout.write(line.replace('X','"').replace('Y','","').replace('Z','","')+'"')
# why does +'"' put the last " at start of the next line down?
#close input and output files
fin.close()
fout.close()

Input
X -6.0000Y -1.2000Z -7.6000
X -6.0000Y -0.0293Z -7.6000
X -6.0000Y -0.0213Z -7.6000

What I Get with above code
" -6.0000"," -1.2000"," -7.6000
"" -6.0000"," -0.0293"," -7.6000
"" -6.0000"," -0.0213"," -7.6000"

What I Want
" -6.0000"," -1.2000"," -7.6000"
" -6.0000"," -0.0293"," -7.6000"
" -6.0000"," -0.0213"," -7.6000"

Thanks All




2 REPLIES 2
Message 2 of 3
KrisKaplan
in reply to: mmharris

Each line read from the file will be line ending character, and with the Universal line endings (default) that line ending would be the \n character. So when you append anything to the end of a line read from a file like this, you are appending after the newline character. So instead of appending " at the end of the line, you could just replace the line ending.

 

fout.write(line.replace('X','"').replace('Y','","').replace('Z','","').replace('\n', '"\n')

 

But looking at what you want to do, I might instead go with a regex and capture the x, y, and z values and write out the line as a formatted string exactly like you want.

 

Kris



Kris Kaplan
Message 3 of 3
mmharris
in reply to: mmharris

THANK YOU

 

your change from my code of:  

+ ' " '   

to your code of:

.replace('\n', '"\n'))

solves most or all of the problem

I notice that the first and last lines of code seem to have a space before the  "

but I can manually edit that out in the 2 lines

whereas manually editing 2000 + lines of code many times is a nonstarter

PLUS for all I know the software I'm trying to alter the file for will be fine with the 2 spaces

or might not work at all for other reasons

 

1 step forward thank you

 

as a beginning python programmer

1) I did not know about default line ending character

2) my python  vocabulary and programing knowledge is so rudimentary I can't understand

    most of what you wrote BUT your 1 line of code is easy to follow

 

FYI

running the following 

 

X -6.0000Y -1.2000Z -7.6000
X -6.0000Y -0.0293Z -7.6000
X -6.0000Y -0.0213Z -7.6000
X -6.0000Y -0.0209Z -7.6000
X -6.0000Y -0.0129Z -6.9592
X -6.0000Y -0.0049Z -6.9484
X -6.0000Y 0.0031Z -6.9312
X -6.0000Y 0.0111Z -6.9204
X -6.0000Y 0.0191Z -6.9069
X -6.0000Y 0.0271Z -6.8960
X -6.0000Y 0.0351Z -6.8849
X -6.0000Y 0.0431Z -6.8740
X -6.0000Y 0.0511Z -6.8632
X -6.0000Y 0.0591Z -6.8530
X -6.0000Y 0.0671Z -6.8439
X -6.0000Y 0.0751Z -6.8346
X -6.0000Y 0.0831Z -6.8258
X -6.0000Y 0.0911Z -6.8169
X -6.0000Y 0.0991Z -6.8086
X -6.0000Y 0.1071Z -6.8008
X -6.0000Y 0.1151Z -6.7933
X -6.0000Y 0.1231Z -6.7855
X -6.0000Y 0.1311Z -6.7777
X -6.0000Y 0.1391Z -6.7709
X -6.0000Y 0.1471Z -6.7640
X -6.0000Y 0.1551Z -6.7572
X -6.0000Y 0.1631Z -6.7504
X -6.0000Y 0.1711Z -6.7441
X -6.0000Y 0.1791Z -6.7373
X -6.0000Y 0.1871Z -6.7311
X -6.0000Y 0.1951Z -6.7252
X -6.0000Y 0.2031Z -6.7195
X -6.0000Y 0.2111Z -6.7138

 

now i get  ( space " 1st line  and no" last line) 

not worth fixing at this time since easy manual edit but interesting 

thank you 1 more time

 

" -6.0000"," -1.2000"," -7.6000 "
" -6.0000"," -0.0293"," -7.6000"
" -6.0000"," -0.0213"," -7.6000"
" -6.0000"," -0.0209"," -7.6000"
" -6.0000"," -0.0129"," -6.9592"
" -6.0000"," -0.0049"," -6.9484"
" -6.0000"," 0.0031"," -6.9312"
" -6.0000"," 0.0111"," -6.9204"
" -6.0000"," 0.0191"," -6.9069"
" -6.0000"," 0.0271"," -6.8960"
" -6.0000"," 0.0351"," -6.8849"
" -6.0000"," 0.0431"," -6.8740"
" -6.0000"," 0.0511"," -6.8632"
" -6.0000"," 0.0591"," -6.8530"
" -6.0000"," 0.0671"," -6.8439"
" -6.0000"," 0.0751"," -6.8346"
" -6.0000"," 0.0831"," -6.8258"
" -6.0000"," 0.0911"," -6.8169"
" -6.0000"," 0.0991"," -6.8086"
" -6.0000"," 0.1071"," -6.8008"
" -6.0000"," 0.1151"," -6.7933"
" -6.0000"," 0.1231"," -6.7855"
" -6.0000"," 0.1311"," -6.7777"
" -6.0000"," 0.1391"," -6.7709"
" -6.0000"," 0.1471"," -6.7640"
" -6.0000"," 0.1551"," -6.7572"
" -6.0000"," 0.1631"," -6.7504"
" -6.0000"," 0.1711"," -6.7441"
" -6.0000"," 0.1791"," -6.7373"
" -6.0000"," 0.1871"," -6.7311"
" -6.0000"," 0.1951"," -6.7252"
" -6.0000"," 0.2031"," -6.7195"
" -6.0000"," 0.2111"," -6.7138

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report