row.split将逗号分隔的字符串作为一列

时间:2018-10-31 10:08:40

标签: python python-3.x csv split

我有两列-WktLND_STATUS作为csv文件。 我想将此作为两列插入数据库。但是,WKT列的值为,。我如何使用row.split(,)但忽略字符串,中的wkt

需要将完整的字符串插入一列。

 batch.put_item(Item={
                            'WKT': row.split(',')[0],
                            'LND_STATUS': row.split(',')[1]})

WKT LND_STATUS
MULTIPOLYGON (((103.869724493054 1.36120318421679,103.869710781572 1.36118900486298,103.869697400306 1.36120191041511,103.869690996084 1.36119528058237,103.869648868277 1.36123584376291,103.869637050138 1.36122339265608,103.869642642738 1.36121800704654,103.869623683539 1.36119803361899,103.869618044214 1.36120346437506,103.869581136828 1.36116458537731,103.869611330112 1.36113551055288,103.869618971644 1.3611434190657,103.869643665633 1.36111959860761,103.869608828588 1.36108357288402,103.869583781366 1.36110816661464,103.869590290686 1.36111488948709,103.869548566305 1.36115585882782,103.869657705349 1.36126876523403,103.869724493054 1.3612031821679)))  P LAND
MULTIPOLYGON (((103.88155761112 1.36080286570841,103.881439254321 1.36092432229536,103.881467976679 1.36095308237611,103.881492380399 1.3609283664127,103.881508135246 1.36094412199864,103.881516071228 1.36093608110234,103.88159630643 1.36085448775854,103.881575131181 1.36083298791314,103.88158135 1.36082668979684,103.88155761112 1.36080286570841)))  P LAND

LND_STATUS应该具有P LAND,而wkt应该具有P LAND以外的完整字符串。

我的输入是一个csv,并使用row.split(,)访问单个列。我需要 使用row.split(,),但跳过第一列中的,

1 个答案:

答案 0 :(得分:0)

您的CSV除标题以外为' '的双倍空格。检查是否只是错字。

wkt_, lnd_status_ = row.split('  ')

编辑: 解析wkt_部分。

我建议为此使用一个库(在此示例中为Shapely

import shapely.wkt

row = 'MULTIPOLYGON (((103.869724493054 1.36120318421679,103.869710781572 1.36118900486298,103.869697400306 1.36120191041511,103.869690996084 1.36119528058237,103.869648868277 1.36123584376291,103.869637050138 1.36122339265608,103.869642642738 1.36121800704654,103.869623683539 1.36119803361899,103.869618044214 1.36120346437506,103.869581136828 1.36116458537731,103.869611330112 1.36113551055288,103.869618971644 1.3611434190657,103.869643665633 1.36111959860761,103.869608828588 1.36108357288402,103.869583781366 1.36110816661464,103.869590290686 1.36111488948709,103.869548566305 1.36115585882782,103.869657705349 1.36126876523403,103.869724493054 1.36120318421679)))  P LAND'

wkt_, lnd_status_ = row.split('  ')

multypolygone = shapely.wkt.loads(wkt_)
for polygone in multypolygone:
    for lng, lat in polygone.exterior.coords:
        print((lng, lat))

输出

(103.869724493054, 1.36120318421679)
(103.869710781572, 1.36118900486298)
(103.869697400306, 1.36120191041511)
(103.869690996084, 1.36119528058237)
(103.869648868277, 1.36123584376291)
(103.869637050138, 1.36122339265608)
(103.869642642738, 1.36121800704654)
(103.869623683539, 1.36119803361899)
(103.869618044214, 1.36120346437506)
(103.869581136828, 1.36116458537731)
(103.869611330112, 1.36113551055288)
(103.869618971644, 1.3611434190657)
(103.869643665633, 1.36111959860761)
(103.869608828588, 1.36108357288402)
(103.869583781366, 1.36110816661464)
(103.869590290686, 1.36111488948709)
(103.869548566305, 1.36115585882782)
(103.869657705349, 1.36126876523403)
(103.869724493054, 1.36120318421679)