我正在解析URL并将它们保存到文件中。我的代码工作正常的Windows,但在Ubuntu它增加了一个小“U”每一行正确地将输出格式化为文件
import re
reports = "C:\Users/_____/Desktop/Reports/"
string = "Here is a string to test. http://www.blah.com & http://2nd.com"
url_match = re.findall(r'(https?://[^\s]+)', string)
print url_match
if url_match != []:
with open(reports + "_URLs.txt", "a") as text_file:
text_file.write('{}'.format(url_match).replace(',', "\n").replace('[', '').replace(']', '').replace("'", '').replace(' ', '').__add__("\n"))
的前面有没有人对如何解决这种想法?谢谢
怎么样'text_file.write(“{}”格式(url_match).replace(“”, “\ n”)。replace('[','').replace(']','').replace(''“,'').replace('','').__ add __(”\ n “)[1:])(最后注意'[1:]') – inspectorG4dget
''{}'。format(url_match)'就是'url_match'。 – TigerhawkT3
另外,您应该使用'+'而不是'.__ add __()'。 – TigerhawkT3