0

I have two folders, and each folde contains 196 files, they all are in ('\xae\xae\xb4\x9e\x8f\x9f\xba\xc1\xd5\xbd\xcd\xa1\xb7\') format. I am trying to read this data convert it into human readable form. I want to combine the data of both the files of the 2 folder.

I tried this using ord() function but while trying to retrive a single file with expected output, I am getting wrong values. I tries to extract first element of the read but output I am getting is forst value of all the files. here is my code:

for file_name, files in izip(list_of_files, list_of_filesO):
     fi = open(file_name,"r").read()
     fo = open(files,"r").read()
     f =  [open("/home/vidula/Desktop/project/ori_tri/input_%i.data" %i,'w')for i in range(len(list_of_files))]
     read = [ord(i) for i in fi]
     reado = [ord(i) for i in fo]
     zipped = zip (read,reado)
     print read[0]

Expected output:

125,25
36,54
98,36
78,56

Thank you in anticipation.

shreya
  • 297
  • 3
  • 7
  • 15
  • "they all are in ('\xae\xae\xb4\x9e\x8f\x9f\xba\xc1\xd5\xbd\xcd\xa1\xb7\') format." I have absolutely no idea what you mean by this. If I opened your file in Notepad, would I see the quotes and backslashes? Would it all be one long string? – Karl Knechtel Dec 07 '12 at 07:23
  • it is a hex file, cannot be read in notepad. – shreya Dec 07 '12 at 08:50

1 Answers1

0
[ord(i) for i in fi]

Iterating over a file iterates over lines of the file. It sounds like you want to iterate a character at a time. For that, you could try explicitly reading each file as a single chunk, and then iterating over the resulting strings.

If you do not want the whole file contents in memory, you could try making a custom iterator, like:

def each_char_of(a_file):
    while True:
        x = a_file.read(1)
        if not x: return
        yield x
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • I am sorry sir, I am newbie in programming, I am not getting what should I do? I have 2 directory containing 196 files each, In each files 121 values in hex format, I am trying to convert hex to int using ord().and write it into new output file. Each output file should have 121 values of 2 input files(like 123,456 56,23 89,56) and 196 files in new directory.but I am getting 196 values in each file of new directory. Can you please suggest me what shall be done? Thakyou – shreya Dec 07 '12 at 09:05