当前列出的.replace()解决方案未修复我的问题

2024-09-28 05:18:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚尝试了前面一个类似于我的问题所建议的语法,但它对我不起作用。我试过:

newvar = str(oldvar)
newvar = newvar.replace('\r', '').replace('\n', ' ')

我试过:

newvar = oldvar.replace('\r', '').replace('\n', ' ')

我的完整(相关)代码部分如下:

URLS = myurls2.values()

# Retrieve a single page and report the url and contents
def load_url(key, url, timeout):
    conn = urllib.request.urlopen(url, timeout=timeout)
    return conn.readall()


# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
    # Start the load operations and mark each future with its URL
    future_to_url = {executor.submit(load_url, key, url, 60): (key, url)
                 for key, url in myurls2.items()}
    c = 0


    for future in concurrent.futures.as_completed(future_to_url):
        key, url = future_to_url[future]    

        try:
            data = str(future.result())
            data2 = str(data)
            data2 = data2.replace('\r', '').replace('\n', ' ')
            print(data2)
            d = open(filepath,"w")
            d.write(data2)
            d.close()

字典值被转换为URL,然后提交到相关网站,最后我希望将它们转换为字符串,并删除replace语句中列出的字符

谢谢


Tags: andthetokeyurlwithtimeoutload

热门问题