473,395 Members | 1,675 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

write integers to txt file with .write() func in Python?

I am trying to write a Program that takes a string and converts it to Ascii, then writes the numbers to a txt file. However when I try to use the .write() function python throws an error "TypeError: argument 1 must be string or read-only character buffer, not int"
This is an example of the code section I am trying to create:

Expand|Select|Wrap|Line Numbers
  1. print "enter string"
  2. word = raw_input()
  3.  
  4. out_file = open("code.txt","a")
  5. out_file.write("------------------------NEW-CODE--------------------\n")
  6.  
  7. for char in word:
  8.     code= ord(char)   
  9.     out_file.write(code)   #this is the error...
  10.  
  11. out_file.close()
can I use the write() func, or should I use a different function, or do i have to create my own....and if so how would I do that?
Any help greatly appreciated
Dec 18 '06 #1
4 3837
bartonc
6,596 Expert 4TB
I am trying to write a Program that takes a string and converts it to Ascii, then writes the numbers to a txt file. However when I try to use the .write() function python throws an error "TypeError: argument 1 must be string or read-only character buffer, not int"
This is an example of the code section I am trying to create:

Expand|Select|Wrap|Line Numbers
  1. print "enter string"
  2. word = raw_input()
  3.  
  4. out_file = open("code.txt","a")
  5. out_file.write("------------------------NEW-CODE--------------------\n")
  6.  
  7. for char in word:
  8.     code= ord(char)   
  9.     out_file.write(code)   #this is the error...
  10.  
  11. out_file.close()
can I use the write() func, or should I use a different function, or do i have to create my own....and if so how would I do that?
Any help greatly appreciated
To make that work, use

>>> str(ord("a"))
'97'
>>>
to convert the ascii value into something that a text file can handle (text).
You can't mix text and binary data in a file (it's one or the other).
Dec 18 '06 #2
thanks very much, I will see what I can do. I will be sure to let you know if I get it working. ^_^
Dec 18 '06 #3
Got it, thanks a bunch!!!
I didn't understand that the Ascii code was not a "text number", thanks for the help.
Dec 19 '06 #4
bartonc
6,596 Expert 4TB
Got it, thanks a bunch!!!
I didn't understand that the Ascii code was not a "text number", thanks for the help.
You are quite welcome. That's what this site is all about. Keep posting,
Barton
Dec 19 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Greg Yasko | last post by:
Hi. Does anyone know if there's an equivalent of Perl's file::find module in Python? It traverses a directory. I've googled extensively and checked this newsgroup and can't find anything like it...
90
by: Mark Hahn | last post by:
"Michael Geary" <Mike@Geary.com> wrote ... >Does anyone have some sample code where obj$func() would be used? > (Apologies if I missed it.) There have been so many messages about delegation...
2
by: OvErboRed | last post by:
Hi, I'm trying to determine whether a given URL exists. I'm new to Python but I think that urllib is the tool for the job. However, if I give it a non-existent file, it simply returns the 404 page....
0
by: Nancy | last post by:
Hi, I'm running 2.4 Python. I have an extension program that calls C funcs. Actually I have a C prog that calls python that calls C. In a python to C function call I get a SIGSEGV and this stack...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.