Can the comments produce errors in python? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Can the comments produce errors in python? (/thread-22626.html) |
Can the comments produce errors in python? - newbieAuggie2019 - Nov-20-2019 Hi! I'm attaching the outputs of my little programs at the end of the code, as comments, to see quicker what the program does, and also to include ideas, variations, possible errors, etc. to improve my knowledge, and I have just found something that I find strange. This program runs fine: import re string1 = 'Agent Cheesecake was a double agent.' agentNamesRegex = re.compile(r'Agent (\w)\w*') mo0 = agentNamesRegex.sub(r'\0**', string1) print(f"This is string1 '{string1}' with r'\0**' after censorship: {mo0}\n")and produces the following output: But if I attach the output as comments at the end of the code, and I run the new program with the comments again:import re string1 = 'Agent Cheesecake was a double agent.' agentNamesRegex = re.compile(r'Agent (\w)\w*') mo0 = agentNamesRegex.sub(r'\0**', string1) print(f"This is string1 '{string1}' with r'\0**' after censorship: {mo0}\n") ##Output: ## ##This is string1 'Agent Cheesecake was a double agent.' with r' **' after censorship: ** was a double agent. ## ##>>>it produces the following I thought that the comments couldn't produce errors, as I thought once the signs ## were found, python took everything else as comments, and did nothing about what there was inside those comments. Am I wrong? How can the comments produce an error?All the best, RE: Can the comments produce errors in python? - buran - Nov-20-2019 I am not able to reproduce this error. The last snippets works just fine RE: Can the comments produce errors in python? - newbieAuggie2019 - Nov-20-2019 (Nov-20-2019, 03:04 PM)buran Wrote: I am not able to reproduce this error. The last snippets works just fine Hi! I think you just copied and pasted the last piece of code, and run it. I have just made that and it works just fine, as you said. The problem arises when you run the first program and then attach the output to that program as comments. Then it seems that the space in r' **' in the comments produces the error. I have just tested in Cmder and with Cmder it works fine. Not with IDLE, and when I tried to copy and paste the output to the program in PyScripter, it only copies the comments like this: ##Output: ## ##This is string1 'Agent Cheesecake was a double agent.' with r'stopping at the apostrophe, just before the space, that python takes as a null byte. Thank you and all the best, RE: Can the comments produce errors in python? - buran - Nov-20-2019 It looks like something with IDLE RE: Can the comments produce errors in python? - newbieAuggie2019 - Nov-20-2019 (Nov-20-2019, 03:51 PM)buran Wrote: It looks like something with IDLE It seems that it is not only with IDLE, as with PyScripter, it stops the paste process at the point IDLE takes as a null byte. Maybe it's something related to r'\0**' being converted into r' **' instead of being printed as r'\0**' inside the string? If I run actually the program: import re string1 = 'Agent Cheesecake was a double agent.' agentNamesRegex = re.compile(r'Agent (\w)\w*') mo0 = agentNamesRegex.sub(r'\0**', string1) print(f"This is string1 '{string1}' with r'\0**' after censorship: {mo0}\n")with PyScript, the output is the following: so it doesn't even finish printing and executing the whole program.On another level, I know that IDLE is not a very good terminal, but it's something like driving a Mercedes and not using its original brakes. All the best, RE: Can the comments produce errors in python? - snippsat - Nov-20-2019 The problem is that you use \0 escape character that is a zero byte.Inside f-string the r want work then nothing get printed after zero byte.Can trick it together so it kind of work,but make it easier for yourself and don't use zero byte escape character as a replace. import re string1 = 'Agent Cheesecake was a double agent.' agent_names_regex = re.compile(r'Agent (\w)\w*') mo_0 = agent_names_regex.sub(r'\0**', string1) print(fr"This is string1 '{string1}' with \0** after censorship: {mo_0!r}")
RE: Can the comments produce errors in python? - newbieAuggie2019 - Nov-21-2019 (Nov-20-2019, 05:44 PM)snippsat Wrote: The problem is that you useThank you! I had a feeling that it was something like that (although I don't know exactly what a null or zero byte is, I guess it's an empty byte, though not sure what that means either). Once again, I find curious the different outputs while using different terminals: 1) If I try to attach the output of the original program, as comments to it, in PyScript, the program prevents me doing it, and accepts only this paste: ##Output: ## ##This is string1 'Agent Cheesecake was a double agent.' with r'I have to type manually r' **' and then, if I try to paste the rest of the output (I'm very lazy at typing ), the program prevents me again doing it, and accepts only:after censorship:I have once again to type manually the rest: ** was a double agent. If I run the program in PyScript (with or without the comments), this is the strange output (I say strange, because it's part of making exercises with regex and substitution, the program working fine (without the comments) even in IDLE): 2) With Cmder, the program (with or without comments) works fine.3) With IDLE the program (without the comments) works fine, but when I add the output as comments to it, and then try to run it again, the program throws back the message: and that's my main question: Why is that the output added to the program as comments throws back an error message if supposedly python overlooks everything that it's inside the comments preceded by ##?All the best, RE: Can the comments produce errors in python? - snippsat - Nov-21-2019 It should work fine everywhere if remove that \0 and have normal characters as replace.The error happens before comments part. Tested VS Code,PyScripter,cmder,IDLE okay. import re string1 = 'Agent Cheesecake was a double agent.' agent_names_regex = re.compile(r'Agent (\w)\w*') mo_0 = agent_names_regex.sub(r'***', string1) print(f"This is string1 '{string1}' with *** after censorship: {mo_0}") ##Output: ## # This is string1 'Agent Cheesecake was a double agent.' with *** after censorship: *** was a double agent. ## ##>>> RE: Can the comments produce errors in python? - newbieAuggie2019 - Nov-21-2019 (Nov-21-2019, 10:35 AM)snippsat Wrote: The error happens before comments part.Thank you! According to my multiple tries in IDLE, before adding the comments, it doesn't seem so. This program runs fine in IDLE: import re string1 = 'Agent Cheesecake was a double agent.' agentNamesRegex = re.compile(r'Agent (\w)\w*') mo0 = agentNamesRegex.sub(r'\0**', string1) print(f"This is string1 '{string1}' with r'\0**' after censorship: {mo0}\n")and produces the following output in IDLE: It is ONLY when I add that output as comments to the program in IDLE, and then I try to run the program with the comments, when the error message of null bytes is thrown back at me. Probably I'm not very good at explaining myself. I don't need/want to modify the program as it runs fine till I add the output as comments. THE ONLY THING THAT I WOULD LIKE TO KNOW IS WHY THE COMMENTS THROW BACK AN ERROR MESSAGE WHILE SUPPOSEDLY PYTHON DOESN'T READ INSIDE THE COMMENTS PRECEDED BY ##.Just to clarify a bit further, the original program to see differences in substitutions is: import re string1 = 'Agent Cheesecake was a double agent.' agentNamesRegex = re.compile(r'Agent (\w)\w*') mo0 = agentNamesRegex.sub(r'\0**', string1) mo1 = agentNamesRegex.sub(r'\1**', string1) print(f"This is string1 '{string1}' with r'\0**' after censorship: {mo0}\n") print(f"This is string1 '{string1}' with r'\1**' after censorship: {mo1}\n")which also runs fine in IDLE, producing the following output: And it also runs fine till I add the output as comments to the program in IDLE (but before that, it runs fine). I only simplify the program to make the question of why those comments are read in IDLE and throw back an error message. I hope to have explained myself better now, sorry for not transmitting my doubt clearer before.All the best, RE: Can the comments produce errors in python? - micseydel - Nov-26-2019 The error seems clear - you can't have null bytes in a Python source file. There's surely some rationale, but whether the source is your keyboard, the output of a script, or anything else, you can't include null bytes in Python scripts. |