[英]Adding extra character when reading a file in Python
I try to check if url is equal with url_auth.我尝试检查 url 是否与 url_auth 相等。
short_url='short.url/001'
url='five'
with open("shortener_db",'r',encoding='utf-8') as file:
for line in file:
item,url_auth = line.split(",",1)
if item == short_url:
shortener(take_url())
if url_auth == url:
print(f"""
This url is already in use!
shortener version of this url: {item}""")
shortener(take_url())
with open('shortener_db','a',encoding='utf-8') as f2:
f2.write(short_url)
f2.write(',')
f2.write(url)
f2.write('\n')
I printed both url and url_auth length, url_auth has an extra length.I suppose, python takes \n as an extra character and url becomes.= url_auth,If this is the case, how can I block this from happening or if that's not the case?我同时打印了 url 和 url_auth 长度,url_auth 有一个额外的长度。我想,python 将 \n 作为一个额外的字符,而 url 变成了.= url_auth,如果是这种情况,我该如何阻止这种情况发生,或者如果不是这样案件? how can I block this and what is the case: this is the shortener_db file:
我怎样才能阻止这个,这是什么情况:这是 shortener_db 文件:
short.url/001,five
short.url/002,five
solved by using line = line.strip('\n')
通过使用
line = line.strip('\n')
解决
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.