Text File

Reply

Join Date: Dec 2006
Posts: 25
Reputation: wandie is an unknown quantity at this point 
Solved Threads: 0
wandie wandie is offline Offline
Light Poster

Text File

 
0
  #1
Apr 24th, 2007
How do i take results in a mutli dim array and write it to a text file. Each sub array goes on a new line. I am a newbie at this.

  1.  
  2. [[2934110, 'B1', 'D4', '7C7C7C7C804040404040F140404000'], [2934110, 5, 1, 1, '01', 'Actes Sud '], [2934110, 4, 1, 2, '8C00Dubbelganger (motief)'], [2934110, 3, 1, 1, '01', '01', '03', 'Les amants imparfaits', ' : roman'], [2934110, 6, 7, '104', 0], [2934110, 6, 1, 1, '01', '01', '03', 'Domain'], [2934110, 2, 1, 1, '01', 0, 'C4', 'Fleutiaux, Pierrette']]

I would was wondering if I can place every subarry in a text file as below each new line in the text file will contain a sub array:

[2934110, 'B1', 'D4', '7C7C7C7C804040404040F140404000']
[2934110, 5, 1, 1, '01', 'Actes Sud ']
[2934110, 4, 1, 2, '8C00Dubbelganger (motief)']
[2934110, 3, 1, 1, '01', '01', '03', 'Les amants imparfaits', ' : roman']
[2934110, 6, 7, '104', 0], [2934110, 6, 1, 1, '01', '01', '03', 'Domain']
[2934110, 2, 1, 1, '01', 0, 'C4', 'Fleutiaux, Pierrette']

Please help
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Text File

 
0
  #2
Apr 24th, 2007
just use the for loop to go over each item, then manipulate from there
  1. >>> for i in alist:
  2. ... print ' '.join(map(str,i))
  3. ...
  4. 2934110 B1 D4 7C7C7C7C804040404040F140404000
  5. 2934110 5 1 1 01 Actes Sud
  6. 2934110 4 1 2 8C00Dubbelganger (motief)
  7. 2934110 3 1 1 01 01 03 Les amants imparfaits : roman
  8. 2934110 6 7 104 0
  9. 2934110 6 1 1 01 01 03 Domain
  10. 2934110 2 1 1 01 0 C4 Fleutiaux, Pierrette
  11. >>>
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 25
Reputation: wandie is an unknown quantity at this point 
Solved Threads: 0
wandie wandie is offline Offline
Light Poster

Re: Text File

 
0
  #3
Apr 27th, 2007
Thanks for the assistance it works. But when I want to put the print results in a text file it only reads one line. I have tried this code but still gives me inputs one line .
  1. for i in results:
  2. valfinal=' '.join(map(str,i))
  3. textfile = file('C:/new_file1.txt','wt')
  4. final =open('C:/new_file1.txt','w')
  5. final.writelines(str(valfinal))
  6. final.close()
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,536
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 170
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Text File

 
0
  #4
Apr 27th, 2007
You have to add a newline character to the end of each line in the string:
  1. mylist = [[2934110, 'B1', 'D4', '7C7C7C7C804040404040F140404000'],
  2. [2934110, 5, 1, 1, '01', 'Actes Sud '],
  3. [2934110, 4, 1, 2, '8C00Dubbelganger (motief)'],
  4. [2934110, 3, 1, 1, '01', '01', '03', 'Les amants imparfaits', ' : roman'],
  5. [2934110, 6, 7, '104', 0], [2934110, 6, 1, 1, '01', '01', '03', 'Domain'],
  6. [2934110, 2, 1, 1, '01', 0, 'C4', 'Fleutiaux, Pierrette']]
  7.  
  8. mystr = ""
  9. for x in mylist:
  10. mystr = mystr + str(x) + '\n'
  11.  
  12. # test
  13. print mystr
  14.  
  15. filename = "data1.txt"
  16. fout = open(filename, "w")
  17. fout.write(mystr)
  18. fout.close()
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Text File

 
0
  #5
Apr 27th, 2007
if you just want to print to one output file, just do a redirect on your command prompt. eg

  1. python yourscript.py > outfile
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 25
Reputation: wandie is an unknown quantity at this point 
Solved Threads: 0
wandie wandie is offline Offline
Light Poster

Re: Text File

 
0
  #6
Apr 30th, 2007
Thanks for the assistance. here is the final
  1. mystr=""
  2. for x in results:
  3. mystr2 = ""
  4. for y in x:
  5. mystr2 = mystr2 + str(y)
  6. mystr = mystr +str(mystr2)+'\n'
  7.  
  8. final =open('C:/new_file1.txt','w')
  9. final.writelines(str(mystr))
  10. final.close()
Last edited by wandie; Apr 30th, 2007 at 5:53 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC