Eremis
January 18th, 2010, 09:39 PM
Hi everybody!
I just started learning python (have a background in java), and I am using a "Learning Python O'REILLY" book. However I tried making a simple script that would convert text to binary, one letter at a time with a for loop...
Here's an example of my logic:
read a file to a string --> convert string to a list (ex. "Hello" --> ['H','e','l','l','o']) --> convert each letter to a int (ex. ord('H')) --> convert each int into bin (ex. bin(num)) --> put all the binary numbers into array --> print them to a diffrent file...
The result:
File1.txt --> Hello
File2.txt --> 0b1001000 0b1100101 0b1101100 0b1101100 0b1101111
Here's my code so far:
#!usr/bin/python26
#
# Description:
# Text to binary converter
# converts using regular ascii code
#
# Developer:
# Eremis
#
import sys
if len(sys.argv) != 2:
print "\n\t Text to Binary Converter 1.0 "
print "\t ---------------------------------------------------\n"
print "\t Usage: ./TextToBin.py <inputFile.txt>\n"
sys.exit(1)
# reading input file...
try:
text = open(sys.argv[1], "r").readlines()
except(IOError):
print "[-] Error: Check your text file path\n"
sys.exit(1)
listText = list(text)
listOutText = []
for c in listText:
listOutText[c] = bin(ord(listText[c]))
print listOutText
Every time I run this I get an error: Why?, and How can I fix it?
Traceback (most recent call last):
File "TextToBin.py", line 31, in <module>
listOutText[c] = bin(ord(listText[c]))
TypeError: list indices must be integers, not str
Thanks for the help1
I just started learning python (have a background in java), and I am using a "Learning Python O'REILLY" book. However I tried making a simple script that would convert text to binary, one letter at a time with a for loop...
Here's an example of my logic:
read a file to a string --> convert string to a list (ex. "Hello" --> ['H','e','l','l','o']) --> convert each letter to a int (ex. ord('H')) --> convert each int into bin (ex. bin(num)) --> put all the binary numbers into array --> print them to a diffrent file...
The result:
File1.txt --> Hello
File2.txt --> 0b1001000 0b1100101 0b1101100 0b1101100 0b1101111
Here's my code so far:
#!usr/bin/python26
#
# Description:
# Text to binary converter
# converts using regular ascii code
#
# Developer:
# Eremis
#
import sys
if len(sys.argv) != 2:
print "\n\t Text to Binary Converter 1.0 "
print "\t ---------------------------------------------------\n"
print "\t Usage: ./TextToBin.py <inputFile.txt>\n"
sys.exit(1)
# reading input file...
try:
text = open(sys.argv[1], "r").readlines()
except(IOError):
print "[-] Error: Check your text file path\n"
sys.exit(1)
listText = list(text)
listOutText = []
for c in listText:
listOutText[c] = bin(ord(listText[c]))
print listOutText
Every time I run this I get an error: Why?, and How can I fix it?
Traceback (most recent call last):
File "TextToBin.py", line 31, in <module>
listOutText[c] = bin(ord(listText[c]))
TypeError: list indices must be integers, not str
Thanks for the help1