python怎么读取图像的txt标注_如何在Python中读取带有标记的txt文件?

这篇博客介绍了如何在Python中使用BeautifulSoup库来读取含有标记的txt文件,特别是针对图像标注的情况。示例代码展示了从txt文件中提取id、human_label、summary和short_text信息,并存储为ShortText实例的过程。
摘要由CSDN通过智能技术生成

我已经通过美容小组解决了。在import codecs

from bs4 import BeautifulSoup

class ShortText:

def __init__(self, my_id, human_label, summary, short_text):

self.id = my_id

self.human_label = human_label

self.summary = summary

self.short_text = short_text

def __str__(self):

'''

For printing purposes.

'''

return '%d\t%d\t%s\t%s' % (self.id, self.human_label, self.summary, self.short_text)

def load_file(filename):

#retrieve the original text

with codecs.open(filename, encoding='utf-8') as f:

data = f.read()

#use beautifulsoup to get tag attributes and elements

soup = BeautifulSoup(data)

tags = soup.find_all('doc')

#store in a dictionary with ShortText Instances as values

instances = {}

my_id = 0

for t in tags:

human_label = int(t.human_label.get_text())

summary = t.summary.get_text().replace("\n", "").replace(" ", "")

short_text = t.short_text.get_text().replace("\n", "").replace(" ", "")

instances[my_id] = ShortText(my_id, human_label, summary, short_text)

my_id +=1

return instances

谢谢你们!在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值