使用Python在字段中分隔逗号分隔的文本
问题描述:
我目前正在尝试使用Python将表转换为RDF,并将每个单元格的值附加到URL的末尾(例如E00变成statistics.data.gov.uk/ ID /统计地理学/ E00)。使用Python在字段中分隔逗号分隔的文本
我可以使用脚本对包含单个值的单元格执行此操作。
FirstCode = row[11]
if row[11] != '':
RDF = RDF + '<http://statistics.data.gov.uk/id/statistical-geography/' + FirstCode + '>.\n'
数据库中的一个字段包含逗号分隔的多个值。 因此,上述代码返回所有附加到URL的代码
例如, http://statistics.data.gov.uk/id/statistical-geography/E00,W00,S00
而我想它返回三个值
statistics.data.gov.uk/id/statistical-geography/E00
statistics.data.gov.uk/id/statistical-geography/W00
statistics.data.gov.uk/id/statistical-geography/S00
有一些代码,让我对这些分离出来?
答
是的,有split
方法。
FirstCode.split(",")
会返回一个列表像(E00, W00, S00)
可以比遍历列表中的项目:
for i in FirstCode.split(","):
print i
会打印出: E00 W00 S00
This page还有其他一些有用的字符串函数
答
for i in FirstCode.split(','):
RDF = RDF + '<http://statistics.data.gov.uk/id/statistical-geography/' + i + '>.\n'
'print RDF +'。\ n'而不是'print i' :) –
Swati
2011-05-20 20:54:48