944,003 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 545
  • Python RSS
Nov 5th, 2009
0

Can Python do this?

Expand Post »
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; Nov 5th, 2009 at 5:32 am.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 5th, 2009
0
Re: Can Python do this?
If you don't already know:
python Syntax (Toggle Plain Text)
  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!
Reputation Points: 2
Solved Threads: 30
Posting Whiz in Training
sravan953 is offline Offline
242 posts
since May 2009
Nov 5th, 2009
0
Re: Can Python do this?
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.
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 5th, 2009
0
Re: Can Python do this?
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.
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009
Nov 5th, 2009
0
Re: Can Python do this?
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:

Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 35
Solved Threads: 22
Junior Poster
ov3rcl0ck is offline Offline
113 posts
since Sep 2009
Nov 5th, 2009
0
Re: Can Python do this?
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 ...
Python Syntax (Toggle Plain Text)
  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; Nov 5th, 2009 at 11:59 am. Reason: titles
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 5th, 2009
0
Re: Can Python do this?
Quote ...
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

Quote ...
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; Nov 5th, 2009 at 1:02 pm.
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Nov 5th, 2009
0
Re: Can Python do this?
Click to Expand / Collapse  Quote originally posted by woooee ...
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.
Reputation Points: 930
Solved Threads: 668
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008
Nov 5th, 2009
0
Re: Can Python do this?
LF and CR are actually printable.

This little code will give you a list of printable characters ...
Python Syntax (Toggle Plain Text)
  1. import string
  2.  
  3. print( list(string.printable) )
Last edited by vegaseat; Nov 7th, 2009 at 11:49 am. Reason: code
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 6th, 2009
0
Re: Can Python do this?
Click to Expand / Collapse  Quote originally posted by woooee ...
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.
Reputation Points: 10
Solved Threads: 1
Junior Poster
P00dle is offline Offline
154 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Ack... drowning... in... parentheses...
Next Thread in Python Forum Timeline: Editing Textfiles





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC