now I have two files: A and B. Both are csv files.
csv A has some data:
a b c 1 2 3 4 5 6 7 8 9 .....
csv B has some data:
b d 7 0 8 3 2 1 .....
now I want to combine the two csv files A and B for a new file C like that:
a b c d 1 2 3 1 7 8 9 3 ......
first I have to read the file with csv.reader,the code is like that:
def open_func(file_name): open_dict={} key_list=[] fd=csv.reader(open(file_name,'r')) j=1 for line in fd: data_len=len(line) if not j: try: for i in range(data_len): open_dict[key_list[i]].append(line[i]) except: #print line continue else: for i in range(data_len): key=line[i] key_list.append(key) for i in range(data_len): open_dict[key_list[data_len-i-1]]=[] j=0 continue return open_dict
I use dict to read them, and I want to key to combine if it is equal
but I don't know how to do then
notice: the data is more than one million rows.
I strongly suggest you to use
pandas
to do this, it provides high-performance, easy-to-use data structures and data analysis tools for Python. So you can try it to process large data.e.g.
Or read file from csv:
and you can write it to file by this:
out.csv: