运行后如何在命令提示符中删除\r\n? [英] How to remove \r\n in command prompt after running?

查看:57
本文介绍了运行后如何在命令提示符中删除\r\n?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我运行代码时.它给了我带有空格的 \r\n .我使用了条带功能,但没有用.如何解决这个问题?这是链接:https://ibb.co/VtVV2fb\

Whenever I run the code. it gives me \r\n with spaces. I used strip function but it didn't work. How to resolve this issue? Here is the link: https://ibb.co/VtVV2fb\

import scrapy
from .. items import FetchingItem

class SiteFetching(scrapy.Spider):
    name = 'Site'
    start_urls = ['https://www.rev.com/freelancers']
    transcription_page = 'https://www.rev.com/freelancers/transcription'

    def parse(self, response):
    items = {
    'Heading': response.css('#sign-up::text').extract(),
    'Earn_steps': response.css('.pb2 .lh-copy::text , .mb1::text , .mb3 .lh-copy::text').extract(), 
    }

    yield response.follow(self.transcription_page, self.trans_faqs, meta={'items':items})

    def trans_faqs(self, response):
    items = response.meta['items']
    names = {
    'name1': 'FAQ1',
    'name2': 'FAQ2', 
    }

    finder = {
    'find1': '#whatentailed p::text , #whatentailed .mr3::text',
    'find2': '#requirements p::text , #requirements .mr3::text',
    }

    for name, find in zip(names.values(), finder.values()):
        items[name] = response.css(find.strip()).extract()
    yield items

推荐答案

strip() 只能去掉字符串末尾的\r\n,不能去掉里面.如果你在文本中有 \r\n 然后使用 text = text.replace(\r\n', '')

strip() can remove \r\n only at the end of string, but not inside. If you have \r\n inside text then use text = text.replace(\r\n', '')

似乎你在 extract() 创建的列表中得到 \r\n 所以你必须使用列表理解从列表中的每个元素中删除

it seems you get \r\n in list created by extract() so you have to use list comprehension to remove from every element on list

data = response.css(find).extract()
data = [x.replace('\r\n', '').strip() for x in data]
items[name] = data

<小时>

删除句子之间的空格和 \r\n 你可以 split('\r\n') 来创建列表用句子.然后你可以strip()每一句话.并且你可以' '.join()把所有的句子都恢复成一个字符串.


to remove spaces and \r\n between sentences you can split('\r\n') to create list with sentences. then you can strip() every sentence. And you can ' '.join() all sentences back to one string.

text = 'Sentence 1\r\n    Sentence 2'

data = text.split('\r\n')
data = [x.strip() for x in data]
text = ' '.join(data)

print(text)

同一行

text = 'Sentence 1\r\n    Sentence 2'

text = ' '.join(x.strip() for x in text.split('\r\n'))

print(text)

re

import re

text = 'Sentence 1\r\n    Sentence 2'

text = re.sub('\r\n\s+', ' ', text)

print(text)

<小时>

for name, find in zip(names.values(), finder.values()):
    data = response.css(find.strip()).extract()
    data = [re.sub('\r\n\s+', ' ', text) for text in data]
    items[name] = data

这篇关于运行后如何在命令提示符中删除\r\n?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆