473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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" "" ""))

6 3069
bvdet
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
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
Okay, so I can use the same loop for that. This works out rather nicely.
Thanks again.
miguel
Dec 15 '10 #7

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

Similar topics

2
by: Jan Rienyer Gadil | last post by:
any idea how to automatically save to a text file? here's what the program do: first, data is read from the serial port every fixed lenght of time the data will then be put to a table, now,...
5
by: mov | last post by:
I am a newbie, please someone tell me how to create a text file and save it using c++. thanks
0
by: Alex Smith | last post by:
Hi Friends, I want to open and read a pdf file from disk And save this content of pdf file into .txt file. In short I want to convert content of pdf into .txt formate and save this text file on...
0
by: Salena | last post by:
Hi: I have 100's of records in my microsoft inbox which i saved into a text file. I need help how to i import all of them into access table through ASP. I try it but my script only read the first...
1
by: riyazrasheed1234 | last post by:
Hi guys, Any one is there to help me? I need to write script my dataset to text file and need to restore that again later to same database file, I am using SQL Server for database. Thanks
1
by: ykhamitkar | last post by:
Hi there, I have a link (CCBill Datalink) which returns me some data in CSV format. Is there any way I can save the contents in a text file automatically when I open the link. Please...
5
by: Joe1986 | last post by:
Hi there, im trying to create a python program that can read a text file line by line and search for specified words/text/strings and remove them from the text file. Then finally save the modified...
3
by: DGee | last post by:
Hi, this is my first post, so be nice on me! I currently have a program that will input and save into a text file. It stores information on the operations of the program, such as inputs, errors...
3
by: Wayne | last post by:
I'm trying to automate the export of a query to a text file using code. If I export the query manually I get the exact result that I want i.e. If I select the query then choose File/Export from...
4
by: Keith G Hicks | last post by:
I'm trying to read a text file and alter the contents of specific lines in the file. I know how to use streamreader to read each line of a file. I'm doing that already to get the data into a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.