将\ n嵌入到另一个单词中的同时从字符串中删除

问题描述 投票:0回答:1

我正在使用搜寻器从网站上下载图像,但由于某些原因,某些文件的标题已嵌入\ n,并带有如下所示的字词:不要抱抱我,我很害怕6(2016 )\ n在YouTube上观看

我尝试使用\ n删除\ n

photo_name = photo_name.replace('\\n','')

photo_name = photo_name.rstrip('\n\r')

但无济于事。有人有建议吗?

-

编辑:这是完整的块代码:

 def get_single_photo(photo):
    print('Grabbing the barcode')
    #According to the IMAGE.01, find the "src" which is include the URL.
    photo_url = photo['src']
    print('Grabbing url: ' + photo_url)
    #Extract movie title to name it.
    photo_name = str(photo['alt'])
    #strip '/' from file name to not conflict with directory
    photo_name = photo_name.replace('/',',')
    photo_name = photo_name.replace('\\n','')
    print('Saving as: ' + photo_name)
    #In order to save it, we need to use (requests.get) to request the photo URL.
    photo_url_requests = requests.get(photo_url)
    #This is a simple way to save photos/images.
    #File directory at Google Drive. Suffix is '.jpg'.
    fileDir = '/content/gdrive/My Drive/Class/ARCH 2238/moviebarcode/'+ photo_name +'.jpg'
    with open(str(fileDir), 'wb') as f:
#      f.write(photo_url_requests.content)
      print('Completed downloading: ' + photo_name)
#      f.close()
python string character immutability
1个回答
0
投票

Nvm我是个白痴,用\ n代替\\ n有效...

© www.soinside.com 2019 - 2024. All rights reserved.