Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Writing ASCII text files

Posted on 2004-08-19
11
Medium Priority
?
3,742 Views
Last Modified: 2010-04-16
I have a list, to which I have appended various strings, simply using myArray.append("hello") for example. Now I need to write a simple routine that works through the array and saves the contents line by line to a text file (ASCII format).

So.... if I appended "apple", "fish" and "pen" to my array, I'd like the text file to have:

apple
fish
pen

Separated each time by a carriage return, so that when I open it in, say, notepad the entries are on successive lines. The other requirement is that Python creates the file - for some reason my script returns the error that the filename/path of the file does not exist, so I need some kind of command that creates the file in the first place.
0
Comment
Question by:mi5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 17

Accepted Solution

by:
ramrom earned 240 total points
ID: 11848217
Without knowing the type of myArray we can't answer your question. Please post the script, or enough of it so we know what you are doing.

We could assume myArray is a list, in which case you could convert the list to a string of lines and write the string to the file:

file('c:/foo.txt', 'w').write('\n'.join(myArray))
0
 
LVL 7

Expert Comment

by:BarryTice
ID: 11852980
def ToFile(theList):
    theFile = open('path_and_filename.txt', 'w')
    for thingie in theList:
        theFile.write(thingie + '\n')
    theFile.close

myList = ['apple', 'fish', 'pen']
ToFile(myList)

The above creates the path_and_filename.txt file in your current directory (or wherever you told it to go) with one entry from your list on each line.

-- b.r.t.
0
 
LVL 17

Expert Comment

by:ramrom
ID: 11853064
My solution and BarryTice's are essentially the same, except his:
- is packaged in a function
- has file operations broken out into explicit open, write, close steps
- uses a for loop instead of join
- puts a return after the last line (which the OP did not request)

Please note that Python convention recommends reserving Capitalized names for Class names. Thus you might use toFile instead of ToFile. Also note that the built_in file function is newer (and perferred?) than open.

Please accept one or the other, as they both answer your question.

Bob Gailer
EE Python Page Editor
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 7

Expert Comment

by:BarryTice
ID: 11853150
(I don't mean to step on toes, ramrom. I recognize that your method would work, too. But it is, perhaps, less clear.)
0
 
LVL 17

Expert Comment

by:ramrom
ID: 11853574
And I did not take it as stepping. Just wanting to clarify for the OP. I tend to appreciate terse code, so offer that, and often will expand or clarify later as needed.
0
 
LVL 9

Expert Comment

by:rjkimble
ID: 11857326
As long as we're commenting on the relative merits of various approaches, I think that ramrom's approach is crystal clear and more in the spirit of Python. That's my two cents. :-)
0
 
LVL 17

Expert Comment

by:ramrom
ID: 11860031
Aw blush
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11902178
ramrom, quick question, does the file get automatically flushed/closed when the object is garbage collected (I would assume so), also, I've not looked in depth at python GC, but how long will this take to get done in general?
0
 
LVL 17

Expert Comment

by:ramrom
ID: 11904554
f = file('asdf','w')
del f # closes file - no need to wait for GC

This discussion is off topic. To pursue it further please open a new question.

Bob Gailer
EE Python Page Editor
0
 
LVL 17

Expert Comment

by:ramrom
ID: 11904564
mi5: 2nd request:

Please accept one or the other, as they both answer your question.

Bob Gailer
EE Python Page Editor
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 11911529
ramrom, not too far off topic, but thanks!
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question