this is my code
with open('txt.txt', 'r') as f:
logs = f.read()
print(logs)
output:
[09-06-2022 15:23:59] (<class 'selenium.common.exceptions.NoSuchElementException'>, NoSuchElementException('no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/table/tbody"}\n (Session info: headless chrome=102.0.5005.63)', None, ['Backtrace:', '\tOrdinal0 [0x0115A8E3 2402531]', '\tOrdinal0 [0x010EB011 1945617]', '\tOrdinal0 [0x00FDC748 837448]', '\tOrdinal0 [0x01009330 1020720]', '\tOrdinal0 [0x010095CB 1021387]', '\tOrdinal0 [0x010363B2 1205170]', '\tOrdinal0 [0x010242F4 1131252]', '\tOrdinal0 [0x010346C2 1197762]', '\tOrdinal0 [0x010240C6 1130694]', '\tOrdinal0 [0x00FFE676 976502]', '\tOrdinal0 [0x00FFF586 980358]', '\tGetHandleVerifier [0x013CBC42 2511938]', '\tGetHandleVerifier [0x013BED6F 2458991]', '\tGetHandleVerifier [0x011F31AA 575914]', '\tGetHandleVerifier [0x011F1FA6 571302]', '\tOrdinal0 [0x010F1DEB 1973739]', '\tOrdinal0 [0x010F6778 1992568]', '\tOrdinal0 [0x010F6865 1992805]', '\tOrdinal0 [0x010FFA41 2030145]', '\tBaseThreadInitThunk [0x76BA6739 25]', '\tRtlGetFullPathName_UEx [0x77CA8FEF 1215]', '\tRtlGetFullPathName_UEx [0x77CA8FBD 1165]', '']), 'Traceback (most recent call last):\n File "maingui.py", line 94, in run\n File "maingui.py", line 249, in <lambda>\n File "maingui.py", line 361, in parser\n File "selenium\\webdriver\\remote\\webdriver.py", line 1251, in find_element\n File "selenium\\webdriver\\remote\\webdriver.py", line 430, in execute\n File "selenium\\webdriver\\remote\\errorhandler.py", line 247, in check_response\nselenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/table/tbody"}\n (Session info: headless chrome=102.0.5005.63)\nStacktrace:\nBacktrace:\n\tOrdinal0 [0x0115A8E3 2402531]\n\tOrdinal0 [0x010EB011 1945617]\n\tOrdinal0 [0x00FDC748 837448]\n\tOrdinal0 [0x01009330 1020720]\n\tOrdinal0 [0x010095CB 1021387]\n\tOrdinal0 [0x010363B2 1205170]\n\tOrdinal0 [0x010242F4 1131252]\n\tOrdinal0 [0x010346C2 1197762]\n\tOrdinal0 [0x010240C6 1130694]\n\tOrdinal0 [0x00FFE676 976502]\n\tOrdinal0 [0x00FFF586 980358]\n\tGetHandleVerifier [0x013CBC42 2511938]\n\tGetHandleVerifier [0x013BED6F 2458991]\n\tGetHandleVerifier [0x011F31AA 575914]\n\tGetHandleVerifier [0x011F1FA6 571302]\n\tOrdinal0 [0x010F1DEB 1973739]\n\tOrdinal0 [0x010F6778 1992568]\n\tOrdinal0 [0x010F6865 1992805]\n\tOrdinal0 [0x010FFA41 2030145]\n\tBaseThreadInitThunk [0x76BA6739 25]\n\tRtlGetFullPathName_UEx [0x77CA8FEF 1215]\n\tRtlGetFullPathName_UEx [0x77CA8FBD 1165]\n\n')
the output i needed
[09-06-2022 15:23:59] (<class 'selenium.common.exceptions.NoSuchElementException'>, NoSuchElementException('no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/table/tbody"}
(Session info: headless chrome=102.0.5005.63)', None, ['Backtrace:', ' Ordinal0 [0x0115A8E3 2402531]', ' Ordinal0 [0x010EB011 1945617]', ' Ordinal0 [0x00FDC748 837448]', ' Ordinal0 [0x01009330 1020720]', ' Ordinal0 [0x010095CB 1021387]', ' Ordinal0 [0x010363B2 1205170]', ' Ordinal0 [0x010242F4 1131252]', ' Ordinal0 [0x010346C2 1197762]', ' Ordinal0 [0x010240C6 1130694]', ' Ordinal0 [0x00FFE676 976502]', ' Ordinal0 [0x00FFF586 980358]', ' GetHandleVerifier [0x013CBC42 2511938]', ' GetHandleVerifier [0x013BED6F 2458991]', ' GetHandleVerifier [0x011F31AA 575914]', ' GetHandleVerifier [0x011F1FA6 571302]', ' Ordinal0 [0x010F1DEB 1973739]', ' Ordinal0 [0x010F6778 1992568]', ' Ordinal0 [0x010F6865 1992805]', ' Ordinal0 [0x010FFA41 2030145]', ' BaseThreadInitThunk [0x76BA6739 25]', ' RtlGetFullPathName_UEx [0x77CA8FEF 1215]', ' RtlGetFullPathName_UEx [0x77CA8FBD 1165]', '']), 'Traceback (most recent call last):
File "maingui.py", line 94, in run
File "maingui.py", line 249, in <lambda>
File "maingui.py", line 361, in parser
File "selenium\webdriver\remote\webdriver.py", line 1251, in find_element
File "selenium\webdriver\remote\webdriver.py", line 430, in execute
File "selenium\webdriver\remote\errorhandler.py", line 247, in check_response
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/table/tbody"}
(Session info: headless chrome=102.0.5005.63)
Stacktrace:
Backtrace:
Ordinal0 [0x0115A8E3 2402531]
Ordinal0 [0x010EB011 1945617]
Ordinal0 [0x00FDC748 837448]
Ordinal0 [0x01009330 1020720]
Ordinal0 [0x010095CB 1021387]
Ordinal0 [0x010363B2 1205170]
Ordinal0 [0x010242F4 1131252]
Ordinal0 [0x010346C2 1197762]
Ordinal0 [0x010240C6 1130694]
Ordinal0 [0x00FFE676 976502]
Ordinal0 [0x00FFF586 980358]
GetHandleVerifier [0x013CBC42 2511938]
GetHandleVerifier [0x013BED6F 2458991]
GetHandleVerifier [0x011F31AA 575914]
GetHandleVerifier [0x011F1FA6 571302]
Ordinal0 [0x010F1DEB 1973739]
Ordinal0 [0x010F6778 1992568]
Ordinal0 [0x010F6865 1992805]
Ordinal0 [0x010FFA41 2030145]
BaseThreadInitThunk [0x76BA6739 25]
RtlGetFullPathName_UEx [0x77CA8FEF 1215]
RtlGetFullPathName_UEx [0x77CA8FBD 1165]
')
its code:
x = '''[09-06-2022 15:23:59] (<class 'selenium.common.exceptions.NoSuchElementException'>, NoSuchElementException('no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/table/tbody"}\n (Session info: headless chrome=102.0.5005.63)', None, ['Backtrace:', '\tOrdinal0 [0x0115A8E3 2402531]', '\tOrdinal0 [0x010EB011 1945617]', '\tOrdinal0 [0x00FDC748 837448]', '\tOrdinal0 [0x01009330 1020720]', '\tOrdinal0 [0x010095CB 1021387]', '\tOrdinal0 [0x010363B2 1205170]', '\tOrdinal0 [0x010242F4 1131252]', '\tOrdinal0 [0x010346C2 1197762]', '\tOrdinal0 [0x010240C6 1130694]', '\tOrdinal0 [0x00FFE676 976502]', '\tOrdinal0 [0x00FFF586 980358]', '\tGetHandleVerifier [0x013CBC42 2511938]', '\tGetHandleVerifier [0x013BED6F 2458991]', '\tGetHandleVerifier [0x011F31AA 575914]', '\tGetHandleVerifier [0x011F1FA6 571302]', '\tOrdinal0 [0x010F1DEB 1973739]', '\tOrdinal0 [0x010F6778 1992568]', '\tOrdinal0 [0x010F6865 1992805]', '\tOrdinal0 [0x010FFA41 2030145]', '\tBaseThreadInitThunk [0x76BA6739 25]', '\tRtlGetFullPathName_UEx [0x77CA8FEF 1215]', '\tRtlGetFullPathName_UEx [0x77CA8FBD 1165]', '']), 'Traceback (most recent call last):\n File "maingui.py", line 94, in run\n File "maingui.py", line 249, in <lambda>\n File "maingui.py", line 361, in parser\n File "selenium\\webdriver\\remote\\webdriver.py", line 1251, in find_element\n File "selenium\\webdriver\\remote\\webdriver.py", line 430, in execute\n File "selenium\\webdriver\\remote\\errorhandler.py", line 247, in check_response\nselenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[1]/div/table/tbody"}\n (Session info: headless chrome=102.0.5005.63)\nStacktrace:\nBacktrace:\n\tOrdinal0 [0x0115A8E3 2402531]\n\tOrdinal0 [0x010EB011 1945617]\n\tOrdinal0 [0x00FDC748 837448]\n\tOrdinal0 [0x01009330 1020720]\n\tOrdinal0 [0x010095CB 1021387]\n\tOrdinal0 [0x010363B2 1205170]\n\tOrdinal0 [0x010242F4 1131252]\n\tOrdinal0 [0x010346C2 1197762]\n\tOrdinal0 [0x010240C6 1130694]\n\tOrdinal0 [0x00FFE676 976502]\n\tOrdinal0 [0x00FFF586 980358]\n\tGetHandleVerifier [0x013CBC42 2511938]\n\tGetHandleVerifier [0x013BED6F 2458991]\n\tGetHandleVerifier [0x011F31AA 575914]\n\tGetHandleVerifier [0x011F1FA6 571302]\n\tOrdinal0 [0x010F1DEB 1973739]\n\tOrdinal0 [0x010F6778 1992568]\n\tOrdinal0 [0x010F6865 1992805]\n\tOrdinal0 [0x010FFA41 2030145]\n\tBaseThreadInitThunk [0x76BA6739 25]\n\tRtlGetFullPathName_UEx [0x77CA8FEF 1215]\n\tRtlGetFullPathName_UEx [0x77CA8FBD 1165]\n\n')'''
print(x)
so i was wondering if there is anyway to convert the text to a str with '''{content}''' so that it can print the contents of the file properly.
CodePudding user response:
When it converts more complex object to string then it may try to create string useful for debuging (or useful to recreate original object from string) - and it may need to shows \n
, \t
, unicode chars
, etc. in this string.
And it can convert \n
to \\n
to show it - because single \
has special meaning in string and it has to use double \\
to put normal \
in string
You may have to convert it back from \\n
to \n
print(logs.replace('\\n', '\n')
or using raw-string
- r'\n'
print(logs.replace(r'\n', '\n')