2017-10-16 5 views
0

Meine Liste ersetzen:Python: n r t in einer Liste ohne solchen Ausgang n n und endet mit n r n t

['\n\r\n\tThis article is about sweet bananas. For the genus to which banana plants belong, see Musa (genus).\n\r\n\tFor starchier bananas used in cooking, see Cooking banana. For other uses, see Banana (disambiguation)\n\r\n\tMusa species are native to tropical Indomalaya and Australia, and are likely to have been first domesticated in Papua New Guinea.\n\r\n\tThey are grown in 135 countries.\n\n\n\r\n\tWorldwide, there is no sharp distinction between "bananas" and "plantains".\n\nDescription\n\r\n\tThe banana plant is the largest herbaceous flowering plant.\n\r\n\tAll the above-ground parts of a banana plant grow from a structure usually called a "corm".\n\nEtymology\n\r\n\tThe word banana is thought to be of West African origin, possibly from the Wolof word banaana, and passed into English via Spanish or Portuguese.\n']

Beispielcode:

import requests 
from bs4 import BeautifulSoup 
import re 
re=requests.get('http://www.abcde.com/banana') 
soup=BeautifulSoup(re.text.encode('utf-8'), "html.parser") 
title_tag = soup.select_one('.page_article_title') 
print(title_tag.text) 
list=[] 
for tag in soup.select('.page_article_content'): 
    list.append(tag.text) 
#list=([c.replace('\n', '') for c in list]) 
#list=([c.replace('\r', '') for c in list]) 
#list=([c.replace('\t', '') for c in list]) 
print(list) 

Nachdem ich eine Webseite gescratet habe, muss ich Daten bereinigen. Ich möchte alle "\r", "\n", "\t" als "" ersetzen, aber ich habe festgestellt, dass ich Untertitel in diesem haben, wenn ich dies tun, Untertitel und Sätze werden zusammen mischen.

Jeder Untertitel beginnt immer mit \n\n und endet mit \n\r\n\t, ist es möglich, dass ich etwas tun kann, um sie in dieser Liste wie \aEtymology\a zu unterscheiden. Es wird nicht funktionieren, wenn ich \n\n und \n\r\n\t separat zu \a ersetze erste Ursache andere Teile könnten die gleichen Elemente wie diese haben \n\n\r und es wird wie \a\r werden. Danke im Voraus!

Antwort

1

Ansatz

  1. Ersetzen Sie die Untertitel auf eine benutzerdefinierte Zeichenfolge, <subtitles> in der Liste
  2. Ersetzen der \n, \r, \t usw. in der Liste
  3. mit dem aktuellen Untertitel die benutzerdefinierte Zeichenfolge ersetzen

Code

l=['\n\r\n\tThis article is about sweet bananas. For the genus to which banana plants belong, see Musa (genus).\n\r\n\tFor starchier bananas used in cooking, see Cooking banana. For other uses, see Banana (disambiguation)\n\r\n\tMusa species are native to tropical Indomalaya and Australia, and are likely to have been first domesticated in Papua New Guinea.\n\r\n\tThey are grown in 135 countries.\n\n\n\r\n\tWorldwide, there is no sharp distinction between "bananas" and "plantains".\n\nDescription\n\r\n\tThe banana plant is the largest herbaceous flowering plant.\n\r\n\tAll the above-ground parts of a banana plant grow from a structure usually called a "corm".\n\nEtymology\n\r\n\tThe word banana is thought to be of West African origin, possibly from the Wolof word banaana, and passed into English via Spanish or Portuguese.\n'] 

import re 
regex=re.findall("\n\n.*.\n\r\n\t",l[0]) 
print(regex) 

for x in regex: 
    l = [r.replace(x,"<subtitles>") for r in l] 

rep = ['\n','\t','\r'] 
for y in rep: 
    l = [r.replace(y, '') for r in l] 

for x in regex: 
    l = [r.replace('<subtitles>', x, 1) for r in l] 
print(l) 

Ausgabe

['\n\nDescription\n\r\n\t', '\n\nEtymology\n\r\n\t'] 

['This article is about sweet bananas. For the genus to which banana plants belong, see Musa (genus).For starchier bananas used in cooking, see Cooking banana. For other uses, see Banana (disambiguation)Musa species are native to tropical Indomalaya and Australia, and are likely to have been first domesticated in Papua New Guinea.They are grown in 135 countries.Worldwide, there is no sharp distinction between "bananas" and "plantains".\n\nDescription\n\r\n\tThe banana plant is the largest herbaceous flowering plant.All the above-ground parts of a banana plant grow from a structure usually called a "corm".\n\nEtymology\n\r\n\tThe word banana is thought to be of West African origin, possibly from the Wolof word banaana, and passed into English via Spanish or Portuguese.'] 
+0

Das ist sehr ordentlich! Und leicht für mich zu lernen und zu verstehen. Nur eine Fragenliste = [r.replace ('', x, 1) für r in Liste], wofür verwende ich? Als ich es entfernte, druckte es das gleiche Ergebnis aus.Nur neugierig :) Danke! – Makiyo

+0

@Makiyo 1 soll das erste Auftreten von allein ersetzen. Wenn Sie 1 entfernen, sind die Untertitel in der Ausgabe identisch. –

0
import re  

print([re.sub(r'[\n\r\t]', '', c) for c in list]) 

Ich glaube, Sie regex verwenden

+0

Ich glaube nicht, das ist eine richtige Antwort, sein "\ n \ r \ t" bedeuten '\ n' oder '\ r' oder '\ t', wenn Sie lese es als "\ n \ r \ t", dann wird der folgende Satz nutzlos sein "beginnend \ n \ n und endet mit \ n \ r \ n \ t .....". Überprüfen Sie sein Beispiel, es gibt kein "\ n \ r \ t" überhaupt –

0

Sie können dies tun, indem Sie mit regulären Ausdrücken:

import re 
subtitle = re.compile(r'\n\n(\w+)\n\r\n\t') 
new_list = [subtitle.sub(r"\a\g<1>\a", l) for l in li] 

\g<1> ist ein Rückreferenzierung auf die (\ w +) in der ersten regulären Ausdruck. Es lässt dich das wiederverwenden was immer da ist.

+0

Hallo Jan! Ich habe es versucht, aber es funktioniert nicht, ich weiß nicht, ob ich es falsch geschrieben habe. Ich habe gerade den gesamten obigen Code hochgeladen :) – Makiyo

+0

Was hat nicht funktioniert? Irgendwelche Fehler? –

+0

AttributeError: 'Response' Objekt hat kein Attribut 'compile' – Makiyo

Verwandte Themen