python按内容分割txt文件,根据逗号和空格在文本文件中分割文本(python)

这篇博客介绍了如何使用Python处理包含大学和地点信息的TXT文件。通过两次遍历文件,首先建立已知地点集合,然后根据逗号分割确定大学和地点。对于没有逗号的行,通过单词匹配来找出地点。最终,实现了从文本中正确地提取大学和地点。
摘要由CSDN通过智能技术生成

I need to parse text of text file into two categories:

University

Location(Example: Lahore, Peshawar, Jamshoro, Faisalabad)

but the text file contain following text:

"Imperial College of Business Studies, Lahore"

"Government College University Faisalabad"

"Imperial College of Business Studies Lahore"

"University of Peshawar, Peshawar"

"University of Sindh, Jamshoro"

"London School of Economics"

"Lahore School of Economics, Lahore"

I have written code that separate locations on the basis of 'comma'. The below code only work for first line of file and prints 'Lahore' after that it give following error 'list index out of range'.

file = open(path,'r')

content = file.read().split('\n')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值