0

Help fix the code. My script sorts into even and odd numbers of coordinates in the list and only works with a list in decimal number format, but I need to fix the code to work with a list in HEX format (hexadecimal number format)

I don't know the Python language well, but I need to add function hex(str)

Here is a list like this List.txt

(0x52DF625,0x47A406E)
(0x3555F30,0x3323041)
(0x326A573,0x5A5E578)
(0x48F8EF7,0x98A4EF3)
(0x578FE62,0x331DF3E)
(0x3520CAD,0x1719BBB)
(0x506FC9F,0x40CF4A6)

Сode:

with open('List.txt') as fin,\
     open('Save+even.txt', 'a') as foutch,\
      open('Save-odd.txt', 'a') as foutnch:
    data = [line.strip() for line in fin]
    nch = [foutnch.write(str(i) + '\n') 
        for i in data if int(i[1:-1].split(',')[1]) % 2]
    ch = [foutch.write(str(i) + '\n') 
        for i in data if int(i[1:-1].split(',')[1]) % 2 != 1]
FOLBY
  • 11
  • 1
  • 3
  • 1
    you have asked several questions here but you have never [accepted](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) an answer. please read up on [how to accept an answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work), – hiro protagonist May 22 '21 at 12:17
  • 1
    You might want to check out `contextlib.ExitStack()` to help clean up the `with` a bit. – JonSG May 22 '21 at 13:46
  • 1
    @JonSG i did not know about [`ExitStack`](https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack). thanks for bringing it up! i will hopefully remember it the next time i do something similar... – hiro protagonist May 22 '21 at 14:14

1 Answers1

1

this may work for you (i used StringIO instead of real files - but added a comment on how you could use that with real files)

in_file = StringIO("""(0x52DF625,0x47A406E)
(0x3555F30,0x3323041)
(0x326A573,0x5A5E578)
(0x48F8EF7,0x98A4EF3)
(0x578FE62,0x331DF3E)
(0x3520CAD,0x1719BBB)
(0x506FC9F,0x40CF4A6)
""")

even_file = StringIO()
odd_file = StringIO()

# with open( "List.txt") as in_file, open("Save-even.txt", "w") as even_file, open("Save-odd.txt", "w") as odd_file:
for line in in_file:
    x_str, y_str = line.strip()[1:-1].split(",")
    x, y = int(x_str, 0), int(y_str, 0)
    if y & 1:  # y is odd
        odd_file.write(line)
    else:
        even_file.write(line)

print("odd")
print(odd_file.getvalue())

print("even")
print(even_file.getvalue())

it outputs:

odd
(0x3555F30,0x3323041)
(0x48F8EF7,0x98A4EF3)
(0x3520CAD,0x1719BBB)

even
(0x52DF625,0x47A406E)
(0x326A573,0x5A5E578)
(0x578FE62,0x331DF3E)
(0x506FC9F,0x40CF4A6)

the trick is to use base 0 when converting a hex string to int: int(x_str, 0),. see this answer.

hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
  • how do I put the list of the List.txt file since it weighs 1GB. And I would like to save the whole result in two files: 'Save + even.txt' / 'Save-odd.txt' – FOLBY May 22 '21 at 12:12
  • as stated in the comments: use the `for` loop inside this `with` statement: `with open( "List.txt") as in_file, open("Save-even.txt") as even_file, open("Save-odd.txt") as odd_file:` and get rid of all the `StringIO` stuff. that was just to make my example here self-contained. – hiro protagonist May 22 '21 at 12:14
  • oh, and the size of your input file it not an issue (well, 1GB should not be an issue on any reasonably recent hardware); i never read the file in entirely - i just iterate over the lines of it. – hiro protagonist May 22 '21 at 14:09
  • I did the same as you described, but I get an error: Traceback (most recent call last): File "D:/script.py", line 8, in even_file.write(line) io.UnsupportedOperation: not writable – FOLBY May 22 '21 at 14:36
  • How to describe code correctly? I am using python3. Like this https://pastebin.com/uuCn69HB – FOLBY May 22 '21 at 14:46
  • oh, my bad. you need to [`open`](https://docs.python.org/3/library/functions.html#open) the files you want to write to (as opposed to the one you only want to read from) with the mode `"w"`. updated in the answer – hiro protagonist May 22 '21 at 17:00
  • Thank you! May God grant you health! – FOLBY May 22 '21 at 18:26
  • you are welcome. but again: if the answer is helpful please [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it. – hiro protagonist May 23 '21 at 06:28