Can Python do this?

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 70
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training

Can Python do this?

 
0
  #1
27 Days Ago
Hi,

I am reading data from a file. The data is mostly text, but has a small bit of binary in it, as well as a linefeed at the end of every block of text. Can Python read all of it and write it to another file, while not changing the binary or the linefeed?

I have tried looking around online, but couldn't find the answer. Daniweb is on of the best Forums out there, so I am asking it here.


Also, is there anyway in Python that you can test whether or not a line of data is binary?
Last edited by P00dle; 27 Days Ago at 5:32 am.
I wanted to ask God for a bike, but I know thats not how he does things, so I stole one and asked for forgiveness.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 231
Reputation: sravan953 has a little shameless behaviour in the past 
Solved Threads: 28
sravan953's Avatar
sravan953 sravan953 is offline Offline
Posting Whiz in Training
 
0
  #2
27 Days Ago
If you don't already know:
  1. (identifier)=open("(file.txt)",'r'(or)'w')
  2. (another_identifier)=(identifier).read()
  3. #That's how you get text from a file which is now stored as a String in (another_identifier)
  4. #Now, mess around!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 70
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training
 
0
  #3
27 Days Ago
No, I didnt already know, since I started Python 2 days ago, and have never done another language before that...

Thank you though.

I'm not going to check for binary anymore, instead I will test every line in the file against a regular expression. That is the end of the file. After that I will use pickle to dump it to a new file.
I wanted to ask God for a bike, but I know thats not how he does things, so I stole one and asked for forgiveness.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 70
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training
 
0
  #4
27 Days Ago
What I asked was if there is anyway to check whether or not input received from a file is in binary.

All your post says is how to read from a file.
I wanted to ask God for a bike, but I know thats not how he does things, so I stole one and asked for forgiveness.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 106
Reputation: ov3rcl0ck is an unknown quantity at this point 
Solved Threads: 11
ov3rcl0ck ov3rcl0ck is offline Offline
Junior Poster
 
0
  #5
27 Days Ago
open it in binary mode. In unix however binary mode isn't needed but using 'wb' or 'rb' instead of 'w' or 'r' doesn't hurt. This should copy the file contents of a file to another no matter the content:

  1. fi=open("filein", 'rb').read()
  2. fo=open("fileout",'wb')
  3. fo.write(fi)
  4. fo.close()

In windows you just gotta use the 'rb'/'wb' flags to read the file contents as binary.
NOTE: sudo doesn't apply to real life situations.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,025
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #6
27 Days Ago
If you read a binary as a text file (mode='r'), you have to be aware that the binary data can inadvertently contain an EOF (End Of File) marker early on, at which point the read would stop. Using a binary read (mode='rb') will prevent this.

Here is a simple way to detect a binary file ...
  1. # use with Python3
  2.  
  3. def is_binary_file(fname):
  4. fin = open(fname, "rb")
  5. data = fin.read()
  6. fin.close()
  7. # a typical binary file contains zero value bytes
  8. if 0 in data:
  9. print("File %s is binary" % fname)
  10. else:
  11. print("File %s is not binary" % fname)
  12.  
  13. # pick a typical binary file like an image file you have
  14. # in the working directory or give the full file path
  15. fname = 'py.ico'
  16. is_binary_file(fname)
  17.  
  18. # pick a text file you have in the working directory
  19. fname = 'Zoomer.txt'
  20. is_binary_file(fname)
If you use Python2 the binary read is different, you have to iterate and use ord().

BTW, thread titles like ...
Can Python do this?
I need help
Help

are meaningless and kind of stupid! Use a tiltle that tells the rest of us what you want.
Last edited by vegaseat; 27 Days Ago at 11:59 am. Reason: titles
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,020
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 286
woooee woooee is online now Online
Veteran Poster
 
0
  #7
27 Days Ago
Also, is there anyway in Python that you can test whether or not a line of data is binary?
All data is binary. That's the way the computer does it. I am assuming that you mean bytes that are not text. If you look at an ASCII table like this one http://www.asciitable.com/ it becomes apparent that for the English character set you want to check for anything less than decimal 32 (space) or greater than decimal 126 (~). Python uses ord(character) to covert and I don't know of any way other than converting and checking every byte in the file. Post some code and we can help you with problems

BTW, thread titles like ...
Can Python do this?
I need help
Help
are meaningless and kind of stupid! Use a tiltle that tells the rest of us what you want.
It takes a large amount of self control not to respond with
Can Python do this? (Who are you, Steve Urkel)
I need help (We all do but most of us can't afford a psychiatrist)
Help (Have you fallen and can't get up?)
Last edited by woooee; 27 Days Ago at 1:02 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 927
Reputation: Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough Gribouillis is a jewel in the rough 
Solved Threads: 216
Gribouillis's Avatar
Gribouillis Gribouillis is online now Online
Posting Shark
 
0
  #8
27 Days Ago
Originally Posted by woooee View Post
It takes a large amount of self control not to respond with
Can Python do this? (Who are you, Steve Urkel)
I need help (We all do but most of us can't afford a psychiatrist)
Help (Have you fallen and can't get up?)
The answer to "Can Python do this ?" is usually Yes.
"I need help" often means it's homework due in 2 hours.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,025
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #9
27 Days Ago
LF and CR are actually printable.

This little code will give you a list of printable characters ...
  1. import string
  2.  
  3. print( list(string.printable) )
Last edited by vegaseat; 25 Days Ago at 11:49 am. Reason: code
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 70
Reputation: P00dle is an unknown quantity at this point 
Solved Threads: 0
P00dle P00dle is offline Offline
Junior Poster in Training
 
0
  #10
25 Days Ago
Originally Posted by woooee View Post
All data is binary. That's the way the computer does it. I am assuming that you mean bytes that are not text. If you look at an ASCII table like this one http://www.asciitable.com/ it becomes apparent that for the English character set you want to check for anything less than decimal 32 (space) or greater than decimal 126 (~). Python uses ord(character) to covert and I don't know of any way other than converting and checking every byte in the file. Post some code and we can help you with problems

It takes a large amount of self control not to respond with
Can Python do this? (Who are you, Steve Urkel)
I need help (We all do but most of us can't afford a psychiatrist)
Help (Have you fallen and can't get up?)
I appreciate all the help, guys. Thank you.
I wanted to ask God for a bike, but I know thats not how he does things, so I stole one and asked for forgiveness.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC