MD file to HTML, remove new line from hex

Question:

Repl link: https://replit.com/@CessT/Myfirstproject#main.py

Hey guys - need your guidance. This is a part of my school assignment, and I’ve been trying to solve this for the last 3 hours. Its fine to not give me direct answers, just direct me how to find the right solution.

I have a markdown file that has hex digits, that needs to be converted to binary file.
I keep getting this error: ValueError: non-hexadecimal number found in fromhex() arg at position 1.

There’s some \n (new line) that keeps appearing in my array (most likely as it was an md file), and cant seem to remove it.

Already tried lstrip, rstrip, negative slicing, replace( ), but still cant get rid of \n.
My original input has to be an md file, and output should be a jpeg.
Please help.

print("\nFile:")
with open('C:\Users\CessT\Desktop\sample.md', 'r') as file:
    data = file.read()
print(data)

print("\nDelete non-hex characters")
''.join(data.replace('\n','').split())
print(data)

print("\nChange hex to binary")
file_bytearray = bytearray.fromhex(data)
print(file_bytearray)

print("\nBinary data to file")
new_file = open("C:\Users\CessT\Desktop\new_file", "wb")
new_file.write(file_bytearray)

This directory won’t exist on replit, as this isn’t Windows, are you running this locally?

1 Like

Yes. I was doing it on pycharm, and just tried it on repl. I could make it as comments if it helps in running it here. Any idea about either converting markdown file to plaintext, or removing \n next to hex arrays?

When you do ''.join(data.replace('\n','').split()), you’re not saving the modified string back to data . You need to reassign the result back to data .

Remember that strings in Python are immutable. Any operation on a string (like replace() , split() , etc.) does not modify the original string but instead returns a new one. So, when you execute data.replace('\n','').split() , you’re getting a new string (or a list, in the case of split() ), but the original data string remains unchanged.

That’s why you need to reassign the result back to data, to update its value.

1 Like

Shouldn’t you add a .jpeg extension to the output file?