954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to write the file???

Hi,
It is my code:

mylist = [['fiss','giss'], ['e','h'], ['d','u'], ['c','t'], ['b','o'], ['a','z']]

for alist in mylist:
    g = ' '.join(alist)
print g

f = open("write.txt", "w")
f.write(g)
f.close()

How will I write all the words in the txt file. it is only writing the last line of the file.

Thanks in advance
Regards

BirdaoGwra
Newbie Poster
21 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 
mylist = [['fiss','giss'], ['e','h'], ['d','u'], ['c','t'], ['b','o'], ['a','z']]

g = ''
for alist in mylist:
    g += ' '.join(alist)
print g

f = open("write.txt", "w")
f.write(g)
f.close()


g = '' # It's empty string

in for loop ...
g = something # It's replaced at every time
g += 'sometext' # Saves text in empty variable "g"

-ordi-
Junior Poster in Training
92 posts since Dec 2009
Reputation Points: 18
Solved Threads: 11
 

There is a lil problem. Now it is writing every words/letter in a single line. It should be like this:

fiss giss
e h
d u
c t
b o
a z


Regards

BirdaoGwra
Newbie Poster
21 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Look at the output -ordi-
fiss gisse hd uc tb oa z
Do you see some character are missing?

I dont know how BirdaoGwra want the output,here are a couple of way.
fissgiss is one word in this,some more work is neede to split that to.
Pickle is way to save and get same output back,look at this.
http://www.daniweb.com/forums/thread343685.html

l = [['fiss','giss'], ['e','h'], ['d','u'], ['c','t'], ['b','o'], ['a','z']]

with open('w.txt', 'w') as f:
    f.writelines(''.join(i) + ' ' for i in l)
#--> fissgiss eh du ct bo az 

#---------------------------------------#
l = [['fiss','giss'], ['e','h'], ['d','u'], ['c','t'], ['b','o'], ['a','z']]

with open('w.txt', 'w') as f:
    f.writelines(''.join(i) + '\n' for i in l)

"""Output-->
fissgiss
eh
du
ct
bo
az
"""


Edit did see you post about output.

l = [['fiss','giss'], ['e','h'], ['d','u'], ['c','t'], ['b','o'], ['a','z']]

with open('w.txt', 'w') as f:
    f.writelines(' '.join(i) + '\n' for i in l)

"""Output-->
fiss giss
e h
d u
c t
b o
a z
"""
snippsat
Practically a Posting Shark
808 posts since Aug 2008
Reputation Points: 353
Solved Threads: 294
 

@snippsat:
Thank you very much.

Regards

BirdaoGwra
Newbie Poster
21 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

Not sure which editor you use, but, with PyScripter (Free), you can run to where the cursor is sitting in your code, then step (F8) line by line and watch the value of your variables change. This is a HUGE help when you're trying to pin down what's going on in loops. :)

Tommymac501
Junior Poster in Training
89 posts since Jun 2010
Reputation Points: 14
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: