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.

[[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:'(

Recommended Answers

All 5 Replies

just use the for loop to go over each item, then manipulate from there

>>> for i in alist:
...  print ' '.join(map(str,i))
... 
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
>>>

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 :| .

for i in results:
      valfinal=' '.join(map(str,i))
       textfile = file('C:/new_file1.txt','wt')
       final =open('C:/new_file1.txt','w')
       final.writelines(str(valfinal))
       final.close()

You have to add a newline character to the end of each line in the string:

mylist = [[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']]

mystr = ""
for x in mylist:
    mystr = mystr + str(x) + '\n'

# test
print mystr

filename = "data1.txt"
fout = open(filename, "w")
fout.write(mystr)
fout.close()

if you just want to print to one output file, just do a redirect on your command prompt. eg

python yourscript.py > outfile

Thanks for the assistance. here is the final:)

mystr=""
            for x in results:
                mystr2 = ""
                for y in x:
                    mystr2 = mystr2 + str(y)
                mystr = mystr +str(mystr2)+'\n'
 
            final =open('C:/new_file1.txt','w')
            final.writelines(str(mystr))
            final.close()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.