python - 从以特定字符开头的文件中删除空记录

标签 python python-3.x string

我有一个包含 DBLP 数据集的文件,该数据集由计算机科学中的书目数据组成。我想删除一些缺少信息的记录。例如,我想删除缺少 field 的记录。在此数据集中, field 后跟“#c”。

在此代码中,我按手稿标题(“#*”)拆分文档。现在,我正在尝试删除没有 field 名称的记录。

输入数据:

#*Toward Connectionist Parsing.

#@Steven L. Small,Garrison W. Cottrell,Lokendra Shastri

#t1982

#c

#index14997


#*A Framework for Reinforcement Learning on Real Robots.

#@William D. Smart,Leslie Pack Kaelbling

#t1998

#cAAAI/IAAI

#index14998

#*Efficient Goal-Directed Exploration.

#@Yury V. Smirnov,Sven Koenig,Manuela M. Veloso,Reid G. Simmons

#t1996

#cAAAI/IAAI, Vol. 1

#index14999

我的代码:

inFile = open('lorem.txt','r')
Data = inFile.read()
data = Data.split("#*")
ouFile = open('testdata.txt','w')
for idx, word in enumerate(data):
    print("i = ", idx)
    if not('#!' in data[idx]):
        del data[idx]
        idx = idx - 1
    else:
        ouFile.write("#*" + data[idx])
ouFile.close()
inFile.close()

预期输出:

#*A Framework for Reinforcement Learning on Real Robots.

#@William D. Smart,Leslie Pack Kaelbling

#t1998

#cAAAI/IAAI

#index14998

#*Efficient Goal-Directed Exploration.

#@Yury V. Smirnov,Sven Koenig,Manuela M. Veloso,Reid G. Simmons

#t1996

#cAAAI/IAAI, Vol. 1

#index14999

实际输出: 一个空的输出文件

最佳答案

str.find将为您提供子字符串的索引,如果子字符串不存在,则为 -1。

DOCUMENT_SEP = '#*'

with open('lorem.txt') as in_file:
    documents = in_file.read().split(DOCUMENT_SEP)

with open('testdata.txt', 'w') as out_file:
    for document in documents:
        i = document.find('#c')
        if i < 0:  # no "#c"
            continue
        # "#c" exists, but no trailing venue information
        if not document[i+2:i+3].strip():
            continue
        out_file.write(DOCUMENT_SEP)
        out_file.write(document)
  • 我没有手动关闭,而是使用了 with 语句。
  • 无需使用索引;在循环中间删除一个项目会使索引计算变得复杂。
  • 使用正则表达式,如 #c[A-Z].. 将使代码更简单。

关于python - 从以特定字符开头的文件中删除空记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57285784/

相关文章:

python - numpy 什么时候停止接受 float 作为索引

jquery - 发送 POST 数据和访问模型时,Django 会生成空白页面

python - 基于用户参数的输出格式

python - 比较 numpy 结构化数组

python - 查找 pandas.Series 中的所有非数字元素

C# 字符串 - 为什么 null 给我的结果与 ""不同?

python - 如何在 Altair 中水平排列两个多面并排图表?

python - Celery apply_async 被多次调用

涉及 map 和 reduce 的 Python 怪异

java - 转换URL GET/POST请求中的特殊字符java