Python:分成几行,并根据搜索删除特定行 - python

我有一个csv文件,如下所示,并以我的一点python知识,我试图将其内容分为基于“ sec”的行作为开始字段,并删除包含sip:+ 99 *,sip:+ 88 *的字段的特定行, s:+ 77 *。

猫text.csv

sec,sip:+1111,2222,3333,4444,5555,sec,6666,sip:+7777,8888,sec,sip:+9999,1000,1100,110,1200,1300,1400

必需的输出是匹配字符串“ sec”的行,并删除任何包含以sip:+ 99 *,sip:+ 88 *和sip:+ 77 *开头的字段的行(在sip:+之后的任何数字) 99xxxx)

拆分后所需的输出:

sec,sip:+1111,2222,3333,4444,5555
sec,6666,sip:+7777,8888
sec,sip:+9999,1000,1100,1100,1200,1300,1400

删除与字段匹配的行后所需的输出:

sec,sip:+1111,2222,3333,4444,5555

我已经尝试过使用csv,re模块进行python代码测试,但是没有运气。
我是python编程的新手,请帮忙。

参考方案

def aggr(s):
  " Aggregate into substrings "
  lst = s.split(',')
  current = [lst[0]]
  result = []
  for i in lst[1:]:
    if i == 'sec':
      if current:
        result.append(','.join(current))
        current = []
    current.append(i)
  if current:
    result.append(','.join(current))

  return result

# Input String
s = 'sec,sip:+1111,2222,3333,4444,5555,sec,6666,sip:+7777,8888,sec,sip:+9999,1000,1100,110,1200,1300,1400'

# Aggregate substrings (i.e. substrings starts with 'sec,sip')
l = aggr(s)
print('\n'.join(l))

# Filter out undesired substrings
prefixes = ['sip:+99', 'sip:+88', 'sip:+77']
# only check third column for match of prefixes
result = [i for i in l if not any(x in i.split(',')[2] for x in prefixes)]
print()
print('\n'.join(result))

输出量

sec,sip:+1111,2222,3333,4444,5555
sec,6666,sip:+7777,8888
sec,sip:+9999,1000,1100,110,1200,1300,1400

sec,sip:+1111,2222,3333,4444,5555
sec,sip:+9999,1000,1100,110,1200,1300,1400

在返回'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 JSON对象必须是str,bytes或bytearray,而不是'dict - python

在Python 3中,要加载以前保存的json,如下所示:json.dumps(dictionary)输出是这样的{"('Hello',)": 6, "('Hi',)": 5}当我使用json.loads({"('Hello',)": 6,…

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

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

Python GPU资源利用 - python

我有一个Python脚本在某些深度学习模型上运行推理。有什么办法可以找出GPU资源的利用率水平?例如,使用着色器,float16乘法器等。我似乎在网上找不到太多有关这些GPU资源的文档。谢谢! 参考方案 您可以尝试在像Renderdoc这样的GPU分析器中运行pyxthon应用程序。它将分析您的跑步情况。您将能够获得有关已使用资源,已用缓冲区,不同渲染状态上…