Reputation: 13
I am trying to use Python to generate a CSV file "distances" where I want to duplicate the rows of another file, "result" like this:
result.csv:
|ID |
|---|
| 1 |
| 2 |
| 3 |
...
distances.csv
|ID1 |ID2 |
|----|----|
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
...
Here is my Python code:
with open('distances.csv', 'wb') as ff:
writer = csv.writer(ff, delimiter=";", quoting=csv.QUOTE_ALL)
with open('result.csv', 'rb') as f:
reader = csv.reader(f, delimiter=";", quoting=csv.QUOTE_NONE)
reader2 = csv.reader(f, delimiter=";", quoting=csv.QUOTE_NONE)
for row in reader:
for row2 in reader2:
lst = []
lst.append(row[0])
lst.append(row[1])
lst.append(row2[0])
lst.append(row2[1])
writer.writerow([unicode(s).encode("utf-8") for s in lst])
For some reason it only duplicates the first row in result.csv
Thanks in advance!
Upvotes: 0
Views: 1612
Reputation: 78554
You can use itertools.product
to make the repetitions on column ID
in distance.csv
:
from itertools import product
import csv
with open('result.csv', 'r') as fin, open('distance.csv', 'w') as fout:
reader = csv.reader(fin, delimiter=";", quoting=csv.QUOTE_NONE)
writer = csv.writer(fout, delimiter=";", quoting=csv.QUOTE_ALL)
# skip header
next(reader)
buffer_ = [row[0] for row in reader]
writer.writerow(['ID1', 'ID2'])
writer.writerows(i for i in product(buffer_, repeat=2))
If you need more than 2 duplications: 'ID1', 'ID2',..., 'IDn'
, change the repeat value in product
to n
Upvotes: 2
Reputation: 150128
Since the csv.reader
is an iterator over the file, you can only go through it once. Try reading the rows into a list:
with open("result.csv", "b") as f, open("distances.csv", "wb") as ff:
reader = csv.reader(f, delimiter=";", quoting=csv.QUOTE_NONE)
writer = csv.writer(ff, delimiter=";", quoting=csv.QUOTE_ALL)
rows = list(reader)
for row1 in rows:
for row2 in rows:
lst = row1 + row2
writer.writerow(lst)
Upvotes: 1