Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from tuple to integer using csv files

Tags:

python

csv

tuples

I'm having a little problem using csv.reader.

I have two files:

FileA corresponds to 9 geographical coordinates (x (column1) and y(column2), with no headers). The file is save from excel into a csv file.

301506  5918202
301012  5919417
309769  5919926
299337  5924043
299602  5924730
305310  5907794
300634  5927108
303968  5922319
303684  5922062
304882  5922009

FileB which is a random sequence of 9 integers between 0 and 8. The file is save as an csv file from excel. The data are in row 1.

6     3     7     8     5     1     2     4     0

here is the code:

import csv

FileA = csv.reader(open(‘FileA.csv’,'rb'),delimeter=',')
coord_list = []
coord_list.extend(FileA)
coordinates = []
for data in coord_list:
     coordinates.append(data[0])
print coordinates

FileB = csv.reader(open(‘FileB.csv’,'rb'),delimeter=',')
seq_list = []
seq_list.extend(FileB)
sequence = []
for data in seq_list:
    sequence.append(data[0])
print sequence


FileC = []

for i range(0,8,1):
         FileC[sequence[i], 1] = coordinates[i,1]
         FileC[sequence[i], 2] = coordinates[i,2]

print FileC

but i get :

File "C:\generatesequence\script.py", line 19, in <module>
    FileC[sequence[i], 1] = coordinates[i,1]
TypeError: list indices must be integers, not tuple

I want to build the FileC to be able to use it after in ArcMAP

Any advice is welcome :)

like image 701
Boris Avatar asked Sep 04 '26 18:09

Boris


2 Answers

a[b, c] indexes a with the tuple (b, c). You need to think hard about what you really want to do at the specific lines where you do this.

like image 167
Ignacio Vazquez-Abrams Avatar answered Sep 07 '26 09:09

Ignacio Vazquez-Abrams


You are try to access list with the tuple. Please try file[sequence[i]: 1] = coordinates[i:1].

like image 26
Nilesh Avatar answered Sep 07 '26 08:09

Nilesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!