By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,035 Members | 1,995 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,035 IT Pros & Developers. It's quick & easy.

How do I save a series of strings to a text file?

P: 39
I'm new at python and programming in general.
I'm currently use AutoCAD at work and am using python to generate scripts that require lots of repetition (drawing pipelines etc.
I am trying to figure out how to save the lines that are generated by my script to a text file.
I currently copy the items I see on the Python Shell to a text file.

I'm thinking what I need to do is somehow, generate a list and then output that list (maybe using the pickle module) to a text file. But I don't know how to automatically generate a list from my output.

If you reply, could you include code examples as I'm really just new at this and am having a rough time working out the details of the code.

Expand|Select|Wrap|Line Numbers
  1. #Autolisp generator to generate list of AutoCommands to create a new layer in AutoCAD
  2.  
  3. #This line of code defines the Lisp Function
  4. print '(defun C:layermaker()' 
  5.  
  6. #This while loop creates a line of Lisp that creates a new layer named Color.
  7. #The variable a is inserted into the string and then loops again.
  8. #The resultant strings are currently copied from the Python Shell into a text file and then ran
  9. #in AutoCAD.  I would like the strings to be outputed to the text file.
  10. a = 0
  11. while a <= 256:
  12.     print '(COMMAND "-LAYER" "MAKE" "COLOR ',a, '" "COLOR" "',a,'" "" "")'
  13.     a = 1+a
  14.  
  15. # I was experimenting with writing to a file here.    
  16. fh = open('helloworld.txt', 'w')
  17. fh.write('hello world 2')
  18. fh.close()
  19.  
Dec 14 '10 #1

✓ answered by bvdet

Miguel,

There is no need in using pickle for your application as simple ascii text should work fine. Most of my scripts in SDS/2 read from and save to an ascii text file each time they are run.

Create each instruction as a string. Save the instruction strings in a list. Write the list to a file. Example:

Expand|Select|Wrap|Line Numbers
  1. fn = "instructions.txt"
  2.  
  3. instructionList = ['(defun C:layermaker()',]
  4. for i in range(256):
  5.     instructionList.append('(COMMAND "-LAYER" "MAKE" "COLOR %s" "COLOR %s" "" "")' % (i, i))
  6.  
  7. f = open(fn, 'w')
  8. f.write("\n".join(instructionList))
  9. f.write(")")
  10. f.close()

Output:
(defun C:layermaker()
(COMMAND "-LAYER" "MAKE" "COLOR 0" "COLOR 0" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 1" "COLOR 1" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 2" "COLOR 2" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 3" "COLOR 3" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 4" "COLOR 4" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 5" "COLOR 5" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 6" "COLOR 6" "" "")
....snip....
(COMMAND "-LAYER" "MAKE" "COLOR 255" "COLOR 255" "" ""))

Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
Miguel,

There is no need in using pickle for your application as simple ascii text should work fine. Most of my scripts in SDS/2 read from and save to an ascii text file each time they are run.

Create each instruction as a string. Save the instruction strings in a list. Write the list to a file. Example:

Expand|Select|Wrap|Line Numbers
  1. fn = "instructions.txt"
  2.  
  3. instructionList = ['(defun C:layermaker()',]
  4. for i in range(256):
  5.     instructionList.append('(COMMAND "-LAYER" "MAKE" "COLOR %s" "COLOR %s" "" "")' % (i, i))
  6.  
  7. f = open(fn, 'w')
  8. f.write("\n".join(instructionList))
  9. f.write(")")
  10. f.close()

Output:
(defun C:layermaker()
(COMMAND "-LAYER" "MAKE" "COLOR 0" "COLOR 0" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 1" "COLOR 1" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 2" "COLOR 2" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 3" "COLOR 3" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 4" "COLOR 4" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 5" "COLOR 5" "" "")
(COMMAND "-LAYER" "MAKE" "COLOR 6" "COLOR 6" "" "")
....snip....
(COMMAND "-LAYER" "MAKE" "COLOR 255" "COLOR 255" "" ""))
Dec 14 '10 #2

P: 39
Thanks very much. What I'm seeing is different code that does the same thing as a while statement.
I guess the %(i,i) is the equivalent as i++ or i = 1?1
Thanks very much as this is very encouraging! It's little things like this that make my day.
Miguel
Dec 14 '10 #3

bvdet
Expert Mod 2.5K+
P: 2,851
The modulo operator (s % d) produces a formatted string where 's' is the format string containing conversion specifiers and 'd' is a tuple or dictionary.
Dec 14 '10 #4

P: 39
After working on that first script, I think I'm addicted.
As part of the lisp routine, I'd like to be able to add code that draws a polyline on the same layer as the one just created.
The LISP command for the Poly line is
(COMMAND "LINE" "0,100" "50,100" "")

So I reused some of the code and came up with the following

Expand|Select|Wrap|Line Numbers
  1. fileName = raw_input("Enter List Name")
  2. fn = fileName+".lsp"
  3.  
  4. instructionList = ['(defun C:layermaker()',]
  5. for i in range(256):
  6.     instructionList.append('(COMMAND "-LAYER" "MAKE" "COLOR %s" "COLOR %s" "" "")' % (i, i))
  7.  
  8. lineList = []
  9. for i in range(256):
  10.     lineList.append('(COMMAND "LINE" "0,%s" "50,%s" "")' % (i, i))
  11.     i=i+50#this attempts to add 50 to the i value so that my lines are spaced 50 units apart vertically.
  12.  
  13. f = open(fn, 'w')
  14. f.write("\n".join(instructionList))
  15. f.write("\n".join(lineList))
  16. f.write(")")
  17. f.close()
  18.  
I created two different lists and then added them to the text file under the same function.
The problem is that the lines are drawn on one layer.
If I can have the commands for the new layer drawn first, then the command to draw the line, the line would be drawn on the layer that was just created, which is what I'm looking to do.
The output would look like this:
(COMMAND "-LAYER" "MAKE" "COLOR 0" "COLOR 0" "" "")
(COMMAND "LINE" "0,0" "50,0" "")
(COMMAND "-LAYER" "MAKE" "COLOR 1" "COLOR 1" "" "")
(COMMAND "LINE" "0,50" "50,50" "")
Is there a way to use the for function to create basically either an array or a nested list to then be able to export the command lines? Thanks.
Miguel
Dec 15 '10 #5

bvdet
Expert Mod 2.5K+
P: 2,851
Use the same loop, and multiply i by 50.
Expand|Select|Wrap|Line Numbers
  1. fn = "instructions.txt"
  2.  
  3. instructionList = ['(defun C:layermaker()',]
  4. for i in range(256):
  5.     instructionList.append('(COMMAND "-LAYER" "MAKE" "COLOR %s" "COLOR %s" "" "")' % (i, i))
  6.     instructionList.append('(COMMAND "LINE" "0,%s" "50,%s" "")' % (i*50, i*50))
  7.  
  8. f = open(fn, 'w')
  9. f.write("\n".join(instructionList))
  10. f.write(")")
  11. f.close()
Dec 15 '10 #6

P: 39
Okay, so I can use the same loop for that. This works out rather nicely.
Thanks again.
miguel
Dec 15 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.