使用特定字符在Python中分割字符串 - python

我正在尝试按特定字符分割输入的文档。我需要在[和]处将它们分开,但我很难弄清楚这一点。

def main():
for x in docread:
    words = x.split('[]')
    for word in words:
        doclist.append(word)

这是将它们分成我的列表的代码部分。但是,它将返回文档的每一行。

例如,我想转换

['I need to [go out] to lunch', 'and eat [some food].']

['I need to', 'go out', 'to lunch and eat', 'some food', '.']

谢谢!

参考方案

您可以尝试使用re.split()代替:

>>> import re
>>> re.split(r"[\[\]]", "I need to [go out] to lunch")
['I need to ', 'go out', ' to lunch']

看起来很奇怪的正则表达式[\[\]]是一个字符类,表示在[]上拆分。内部\[\]必须使用反斜杠转义,因为它们使用与[]相同的字符来包围字符类。

在返回'Response'(Python)中传递多个参数 - python

我在Angular工作,正在使用Http请求和响应。是否可以在“响应”中发送多个参数。角度文件:this.http.get("api/agent/applicationaware").subscribe((data:any)... python文件:def get(request): ... return Response(seriali…

Python exchangelib在子文件夹中读取邮件 - python

我想从Outlook邮箱的子文件夹中读取邮件。Inbox ├──myfolder 我可以使用account.inbox.all()阅读收件箱,但我想阅读myfolder中的邮件我尝试了此页面folder部分中的内容,但无法正确完成https://pypi.python.org/pypi/exchangelib/ 参考方案 您需要首先掌握Folder的myfo…

Python-Excel导出 - python

我有以下代码:import pandas as pd import requests from bs4 import BeautifulSoup res = requests.get("https://www.bankier.pl/gielda/notowania/akcje") soup = BeautifulSoup(res.cont…

R'relaimpo'软件包的Python端口 - python

我需要计算Lindeman-Merenda-Gold(LMG)分数,以进行回归分析。我发现R语言的relaimpo包下有该文件。不幸的是,我对R没有任何经验。我检查了互联网,但找不到。这个程序包有python端口吗?如果不存在,是否可以通过python使用该包? python参考方案 最近,我遇到了pingouin库。

Python ThreadPoolExecutor抑制异常 - python

from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED def div_zero(x): print('In div_zero') return x / 0 with ThreadPoolExecutor(max_workers=4) as execut…