从csv文件创建数组,数据全部在一个字段中

时间:2017-05-15 12:14:53

标签: python arrays csv gis

我的最终目标是将我的lat从已创建的csv文件转换为数组,然后在shp文件中创建多边形。我有10条记录可以创建10个多边形。我不知道如何解决的问题是操纵csv文件。与一个多边形相关联的所有纬度长坐标都在一个字段(单元格)中。即:我的专栏名称为" lat_long"在其中只有一个字段中有15个以上的数字。每2个数字代表多边形点的纬度长度,如下所示:2089,15663,2103,15664等。

我的数据现在看起来像这样:

示例lat_long check prod

1 2071,15600,2089,15663,2103,15664 wfc crush

2 2071,15601,2089,15663,2104,15660 qpf apple

1981,15541,2005,15571,2028,15570 astro tomato

我希望它看起来像这样:

Lat Long Check prod样品

1 20.71 -156 wfc crush

1 20.89 -156.66 wfc crush

1 21.03 -156.64 wfc crush

2 20.71 -156.01 qpf apple

2 20.89 -156.63 qpf apple

2 21.03 -156.64 qpf apple

3 19.81 -155.41 astro tomato

3 20.05 -155.71 astro tomato

3 20.28 -155.7 astro tomato

1 个答案:

答案 0 :(得分:0)

import csv


# Open a new csv to write to
with open("output.csv", "w", newline='') as out_file:
    writer = csv.writer(out_file)

    # Write the header row
    writer.writerow(['Sample', 'lat', 'long', 'check', 'prod'])

    # Open the source .csv file.  You'll need it
    # to be in the same directory as this script.
    with open("data.csv", newline='') as in_file:
        csv_reader = csv.reader(in_file)

        row_num = 0
        for row in csv_reader:
            # Skip the header row of the input data
            if row_num > 0:
                num_columns = len(row)

                sample = int(row[0])
                check = row[num_columns - 2]
                prod = row[num_columns - 1]

                # Loop through the columns by 2 columns at a time
                for col in range(1, num_columns - 2, 2):
                    # Get the raw lat long data
                    raw_lat = row[col]
                    raw_lng = row[col + 1]

                    # Split the lat into components and parse as a float
                    lat = float(raw_lat[0:-2] + '.' + raw_lat[-2:])

                    # Split the long into components and parse as a float
                    lng = float(raw_lng[0:-2] + '.' + raw_lng[-2:])

                    out_row = [sample, lat, lng, check, prod]
                    writer.writerow(out_row)

            row_num += 1