| | |
Can Python do this?
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Nov 2009
Posts: 70
Reputation:
Solved Threads: 0
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?
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.
0
#2 27 Days Ago
If you don't already know:
python Syntax (Toggle Plain Text)
(identifier)=open("(file.txt)",'r'(or)'w') (another_identifier)=(identifier).read() #That's how you get text from a file which is now stored as a String in (another_identifier) #Now, mess around!
•
•
Join Date: Nov 2009
Posts: 70
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Sep 2009
Posts: 106
Reputation:
Solved Threads: 11
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:
In windows you just gotta use the 'rb'/'wb' flags to read the file contents as binary.
Python Syntax (Toggle Plain Text)
fi=open("filein", 'rb').read() fo=open("fileout",'wb') fo.write(fi) 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.
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 ...
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.
Here is a simple way to detect a binary file ...
Python Syntax (Toggle Plain Text)
# use with Python3 def is_binary_file(fname): fin = open(fname, "rb") data = fin.read() fin.close() # a typical binary file contains zero value bytes if 0 in data: print("File %s is binary" % fname) else: print("File %s is not binary" % fname) # pick a typical binary file like an image file you have # in the working directory or give the full file path fname = 'py.ico' is_binary_file(fname) # pick a text file you have in the working directory fname = 'Zoomer.txt' is_binary_file(fname)
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!
•
•
Join Date: Dec 2006
Posts: 1,020
Reputation:
Solved Threads: 286
0
#7 27 Days Ago
•
•
•
•
Also, is there anyway in Python that you can test whether or not a line of data is binary?
•
•
•
•
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.
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
0
#8 27 Days Ago
•
•
•
•
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 need help" often means it's homework due in 2 hours.
0
#9 27 Days Ago
LF and CR are actually printable.
This little code will give you a list of printable characters ...
This little code will give you a list of printable characters ...
Python Syntax (Toggle Plain Text)
import string print( list(string.printable) )
Last edited by vegaseat; 25 Days Ago at 11:49 am. Reason: code
May 'the Google' be with you!
•
•
Join Date: Nov 2009
Posts: 70
Reputation:
Solved Threads: 0
0
#10 25 Days Ago
•
•
•
•
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 wanted to ask God for a bike, but I know thats not how he does things, so I stole one and asked for forgiveness.
![]() |
Similar Threads
- My python program/function! (Python)
- News Story: It’s Like AJAX for Ruby and Python (Ruby)
- News Story: 450 bugs consumed by Python (Python)
- Code Snippet: Mortgage Calculator (Python) (Python)
Other Threads in the Python Forum
- Previous Thread: Ack... drowning... in... parentheses...
- Next Thread: Editing Textfiles
| Thread Tools | Search this Thread |
abrupt ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary directory drive dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib






