我正在尝试比较 result.csv 和* street_segments.csv *中的两列,然后如果它们是* street_segments.csv *到 results.csv <的相同追加列/ em>的
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
我遇到的问题是我得到的是:
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)
使用嵌套的for循环,我不应该得到:
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)
你们能让我知道我做错了什么,或者是否有更好的方法解决这个问题。
所以我的results.csv文件有一行像:
-73.88637197, 40.85400596, 5327502, P-089988, 1015684.082, 250435.3, NO PARKING (SANITATION BROOM SYMBOL) 8:30-10AM TUES & FRI <----->
我的street_segments.csv有一行:
B, P-004958, RANDALL AVENUE, FAILE STREET, COSTER STREET, N
所以我要做的是,如果results.csv的第四列和street_segments.csv的第二列是相同的。我想将street_segments.csv的第3,4,5列添加到results.csv行的末尾。
答案 0 :(得分:0)
您的问题是文件迭代器不会自动&#34;倒带&#34;。也就是说,当您点击外部for
循环的第二次迭代时,永远不会输入内部for
,因为reader
(来自first
)的文件指针位于文件的结尾。要解决这个问题,您需要添加first.seek(0)
命令以返回文件的开头(在内循环之前)。
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