there are two files
one.csv
0, 1, 2, 3
1, a, ww.ok;ww.ant;, anything
2, e, ww.kdi;, ihy
3, se, ww.sdd, sld
4, sd, ww.akd, sdjfa
two.csv
0, 1, 2, 3
1, sd, ww.ok;, 1245
2, 2e3, ww.kdi;, 432
3, de, ww.sdd;, 232
the goal is to first determine whether one.csv column 2 contains two.csv file column 2, and if so, append the data of two.csv column 3 to the end of the one.csv corresponding row. The final result is as follows:
result.csv
0, 1, 2, 3, 4
1, a, ww.ok;ww.ant;, anything 1245
2, e, ww.kdi;, ihy 432
3, se, ww.sdd, sld 232
4, sd, ww.akd, sdjfa
I have written a few lines of code, but I have been stuck in appending this piece, because the actual amount of data to be processed is large, unlike the sample given. If I traverse on the basis of one.csv, the data compared is incomplete. If I traverse on the basis of two.csv, I do not know how to write a file. Ask the elders for advice.
import csv
import datetime
start = datetime.datetime.now()
with open("D:\one.csv") as one:
ic_rd = csv.reader(one)
next(ic_rd)
for i in ic_rd:
with open("D:\two.csv") as ga:
ga_rd = csv.reader(ga)
next(ga_rd)
for g in ga_rd:
if g[2] in i[2]:
i.append(g[2])
break
with open("D:\\result.csv","a+") as rs:
writer = csv.writer(rs)
writer.writerow(i)
end = datetime.datetime.now()
print (end-start)