【问题标题】:Separating comma delimited text within a field using Python使用 Python 在字段中分隔逗号分隔的文本
【发布时间】:2011-05-20 20:47:57
【问题描述】:

我目前正在尝试使用 Python 将表格转换为 RDF,并将每个单元格的值附加到 URL 的末尾(例如,E00 变为 statistics.data.gov.uk/id/statistical-geography/E00)。

我可以使用脚本对包含单个值的单元格执行此操作。

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

是否有一些代码可以让我将它们分开?

【问题讨论】:

    标签: python csv rdf linked-data


    【解决方案1】:

    是的,有split 方法。

    FirstCode.split(",")
    

    会返回一个类似(E00, W00, S00)的列表

    您可以遍历列表中的项目:

     for i in FirstCode.split(","):
          print i
    

    将打印出: E00 W00 S00

    This page 还有一些其他有用的字符串函数

    【讨论】:

    • print RDF + '&lt;http://statistics.data.gov.uk/id/statistical-geography/' + i+ '&gt;.\n 而不是 print i :)
    【解决方案2】:
    for i in FirstCode.split(','):
        RDF = RDF + '<http://statistics.data.gov.uk/id/statistical-geography/' + i + '>.\n'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-17
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多