您可以迭代row
:
import StringIO
import csv
custom_feature_string = 'Custom feature 1;custom feature, 2; Custom feature3; custom "feature" 4; customfeature5'
cfeature = StringIO.StringIO(custom_feature_string)
reader = csv.reader(cfeature, delimiter=';', skipinitialspace=True)
for row in reader:
print '\n'.join(row)
for item in row:
print "X", item, "X"
这产生:
Custom feature 1
custom feature, 2
Custom feature3
custom "feature" 4
customfeature5
X Custom feature 1 X
X custom feature, 2 X
X Custom feature3 X
X custom "feature" 4 X
X customfeature5 X
如果需要,您可以以比用 's 包围数据更复杂的方式操作数据X
,例如将其插入数据库。您可以将行作为一个整体或分段操作,并在每个段或行的修改版本上执行一次数据库插入,或者您喜欢的其他方式。
多行示例
import StringIO
import csv
custom_feature_string = 'Custom feature 1;custom feature, 2; Custom feature3; custom "feature" 4; customfeature5'
multiline = custom_feature_string + "\n" + custom_feature_string
cfeature = StringIO.StringIO(multiline)
reader = csv.reader(cfeature, delimiter=';', skipinitialspace=True)
for row in reader:
print "New line"
for item in row:
print "X", item, "X"
输出
New line
X Custom feature 1 X
X custom feature, 2 X
X Custom feature3 X
X custom "feature" 4 X
X customfeature5 X
New line
X Custom feature 1 X
X custom feature, 2 X
X Custom feature3 X
X custom "feature" 4 X
X customfeature5 X