Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use the write function
#1
Hi everyone!

I'm mew to Python and I'm trying to write a file with Information I get from a finite element mesh in Ansa. For that I use the Ansa python scripts.
I have a file
fileName="essai_tetra"
file=open(fileName+'.mail','w')
And I have a list of element IDs. I would like to write 9 IDs per line but I really don't know how to do that.
I wrote that
for b in range(len(set_elems_ids)):
 file.write("E%d\n" % list(set_elems_ids)[b])
But then I have one ID per line and I don't know how to get 9.

Would someone know how to do that?

Thanks in advance
Manon
Reply
#2
You can try this
with open(filename + '.mail', 'w') as file:
    elems = list(set_elems_ids)
    rows = [elems[i:i+9] for i in range(0, len(elems), 9)]
    for r in rows:
        line = ' '.join('E{:d}'.format(x) for x in r)
        print(line, file=file)
Reply
#3
Hi Gribouillis (nice name by the way!)

Thanks for your reply. That's a great idea, it worked fine! I didn't know that the "for in" function could be used that way, thanks!

Manon
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Better way to write this function SephMon 1 810 Feb-08-2023, 10:05 PM
Last Post: Gribouillis
  write new function or change the old one to work "smartter? korenron 3 1,970 Aug-09-2021, 10:36 AM
Last Post: jamesaarr
  How can I write a function with three parameters? MehmetAliKarabulut 1 2,412 Mar-04-2021, 10:47 PM
Last Post: Larz60+
  How to write a code with İF function? Aycaaxx 1 1,817 Nov-03-2020, 05:46 AM
Last Post: deanhystad
  Print/write to file function tpolim008 4 2,759 Apr-01-2020, 07:59 PM
Last Post: tpolim008
  How to write test cases for a init function by Unit test in python? binhduonggttn 2 3,105 Feb-24-2020, 12:06 PM
Last Post: Larz60+
  write an integer to file using a function xvkxvo 4 3,165 Dec-10-2019, 11:27 PM
Last Post: xvkxvo
  how can I write my function output on CSV file go127a 13 5,482 May-05-2019, 01:55 PM
Last Post: go127a
  Help trying to write a function Rochense 3 4,245 Apr-12-2019, 02:34 PM
Last Post: Gribouillis
  'function' object has no attribute 'write' paramiko lateublegende 1 5,391 Feb-12-2019, 09:03 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020