• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: File Output from Python to Obj-C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: File Output from Python to Obj-C


  • Subject: Re: File Output from Python to Obj-C
  • From: Nicko van Someren <email@hidden>
  • Date: Tue, 16 May 2006 09:26:59 +0100

On 15 May 2006, at 13:36, Wesley wrote:

I'm doing a research project that was prototyped in Python, but is beginning to bog as the numbers get massive, so I'm translating it to Obj-C. It's gone fairly well. I'm stumped, however, by writing to a file. I thought to use NSFileHandle, but I can't seem to figure out how to use it. Can anyone suggest a straight forward way to translate the following:


ft = open('out.xls', 'w')
ft.write("Leaders\tTop\tInteractivity\tContribution\n")

for eachTry in tries:
commCompare = simulateCommunity(C)
line = "%d\t%d\t%d\t%f\n" % (int(A*100), commCompare[0], commCompare[1],\
commCompare[0]/float(commCompare[1]))
ft.write(line)

ft.close()

The key issue you'll have is probably that an NSFileHandle instance only has a -writeData: method, so you need to convert your string into an NSData. This is a matter of choosing an encoding. Since you are working with Python your best bet is probably to use UTF8 encoding, so the lines in the middle of your code would read:
line = [NSString stringWithFormat: @"%d\t%d\t%d\t%f\n", .. stuff here ..];
[fileHandle writeData: [line dataUsingEncoding: NSUTF8StringEncoding]];


Personally, I would (and usually do) address this by adding a category to add methods of writeString: and writeString:withEncoding: to the NSFileHandle class.

I hope this helps.

	Nicko


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >File Output from Python to Obj-C (From: Wesley <email@hidden>)

  • Prev by Date: Thumbnail hints
  • Next by Date: Re: Handling Right Mouse Event for NSTableView
  • Previous by thread: Re: File Output from Python to Obj-C
  • Next by thread: Re: C++ gets translated into ObjC? Strange compiler errors... [RESOLVED]
  • Index(es):
    • Date
    • Thread