I am trying to compare two columns from result.csv and *street_segments.csv* and then if they are the same append columns from *street_segments.csv* to results.csv .
import csv
count=0
count1=0
count2=0
first = file('result.csv', 'rU')
reader = csv.reader(first)
second=file('street_segments.csv', 'rU')
reader1= csv.reader(second)
for row1 in reader1:
count +=1
print count
for row in reader:
count1 += 1
print count1
if row[3]==row1[1]:
row.append(row1[2])
row.append(row1[3])
row.append(row1[4])
count2 += 1
print count2
The issue that I am having is that what I get is:
1 (from count)
1 (from count1)
2 (from count1)
3(from count1)
...
200,000(from count1)
2(from count)
3(from count)
...
90000(from count)
With the nested for loops, shouldn't I be getting:
1 (from count)
1 (from count1)
2(from count1)
...
90000(from count1)
2 (from count)
1 (from count1)
2(from count1)
...
90000(from count1)
3 (from count)
1(from count1)
2(from count1)
...
90000(from count1)
Can you guys let me know what I am doing wrong or if there is a better approach to this problem.
So my results.csv file has a row like:
-73.88637197, 40.85400596, 5327502, P-089988, 1015684.082, 250435.3, NO PARKING (SANITATION BROOM SYMBOL) 8:30-10AM TUES & FRI <----->
and my street_segments.csv has a row:
B, P-004958, RANDALL AVENUE, FAILE STREET, COSTER STREET, N
So what I am trying to do is that if the fourth column of results.csv and the second column of street_segments.csv are the same. I want to add columns 3, 4,5 of street_segments.csv to the end of the row of results.csv.
Your problem is that file iterators do not automatically "rewind". That is, when you hit your second iteration of the outer for
loop, the inner for
is never entered, because the file pointer for reader
(from first
) is at the end of the file. To solve this you'll need to add a first.seek(0)
command to get you back to the start of the file (before your inner loop).
import csv
count=0
count1=0
count2=0
first = file('result.csv', 'rU')
reader = csv.reader(first)
second=file('street_segments.csv', 'rU')
reader1= csv.reader(second)
for row1 in reader1:
count +=1
print count
# Rewind to the start of the file in preparation for the next loop
first.seek(0)
for row in reader:
count1 += 1
print count1
if row[3]==row1[1]:
row.append(row1[2])
row.append(row1[3])
row.append(row1[4])
count2 += 1
print count2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.