Separating comma delimited text within a field using Python
I'm currently trying to convert a table into RDF using Python and attach the values from each cell to the end of a URL (eg E00 becomes statistics.data.gov.uk/id/statistical-geography/E00).
I can do this for cells containing a single value using the script.
FirstCode = row[11]
if row[11] != '':
RDF = RDF + '<http://statistics.data.gov.uk/id/statistical-geography/' + FirstCode + '>.\n'
One field within the database contains multiple values that are comma delimited. The code above therefore returns all the codes appended to the URL
e.g. http://statistics.data.gov.uk/id/statistical-geography/E00,W00,S00
Whereas I'd like it to return three values
stat开发者_StackOverflow社区istics.data.gov.uk/id/statistical-geography/E00
statistics.data.gov.uk/id/statistical-geography/W00
statistics.data.gov.uk/id/statistical-geography/S00
Is there some code that will allow me to separate these out?
Yes, there is the split
method.
FirstCode.split(",")
will return a list like (E00, W00, S00)
You can than iterate over the items in the list:
for i in FirstCode.split(","):
print i
Will print out: E00 W00 S00
This page has some other useful string functions
for i in FirstCode.split(','):
RDF = RDF + '<http://statistics.data.gov.uk/id/statistical-geography/' + i + '>.\n'
精彩评论