Python Forum
How can I copy specif cell from one csv to another csv - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How can I copy specif cell from one csv to another csv (/thread-15339.html)



How can I copy specif cell from one csv to another csv - zoe1111 - Jan-13-2019

Hi,
I want to copy 6th column from csv a to the 6th column in csv b. Besides copying, I want to add three blank rows after the copied one in csv b.

For example, if csv a 6th column has:
Output:
Event 1 p_o
csv b 6th column should have:
Output:
1 blank blank blank p_o blank blank blank
My codes copy 6th row in csv a to 4th row in csv b. It also cannot copy any word (e.g. "p_o"), but it can copy number (e.g. 1). Anyone knows how to fix these issues? Or how can I copy specific cell from one csv to the other? I am thinking if I should copy one cell from csv a and three blank cells from csv c to csv b.

Codes:
import csv
2	
3	def para(session, subj, groupnum):
4	    scorefile = str(groupnum) + str('Asns')
5	    print("scorefile: %s" % scorefile)
6	    scoreart = str(groupnum) + str('AK5s')
7	    print("scoreart: %s" % scoreart)
8	 
9	    new_score=r'K:\users\zoey\artscore\\'+scoreart+r'.csv'
10	    New_score_csv=open(new_score,'w')
11	 
12	    score_data=r'K:\users\zoey\\'+scorefile+r'.csv'
13	    print(score_data)
14	 
15	    with open (score_data) as a, open(new_score,'w') as b:
16	        b.write(next(a))
17	        ca=csv.reader(a)
18	        ca_rows=list(a)
19	        cb=csv.writer(b)
20	
21	        for row in range(len(ca_rows)):
22	            cb.writerow(([" "]*3)+[ca_rows[row][-2]]) # replace -2 with actual col if delimiter is working
23	            cb.writerow([" "]*3)

Thanks for your help! Will do next time


RE: How can I copy specif cell from one csv to another csv - ichabod801 - Jan-13-2019

I'm not sure exactly what you are trying to do, and your code is confusing. However, to handle numbers, you need to change ca_rows[row][-2] on line 22 to str(ca_rows[row][-2]). That will convert the numbers to strings so you can write them. The reason it is writing to the fourth column is that you are putting four blank columns before it ([" "]*3). Change the 3 to a 5 (like King Arthur), and that will put it in the sixth column.


RE: How can I copy specif cell from one csv to another csv - zoe1111 - Jan-13-2019

Sorry for the confusing post. I still have trouble to copy the whole string from cell to cell. I try
str(ca_rows[row][-2])
instead of
ca_rows[row][-2]
. However, I got an error message,
Error:
TypeError: can only concatenate list <not "str"> to list
I want to copy both letters or numbers from csv a to csv b. For example, the content in csv a could be "p_o", or it could be "34". Sorry for any more confusions.


RE: How can I copy specif cell from one csv to another csv - ichabod801 - Jan-13-2019

In your original code, you have ca_rows[row][-2] in brackets ([]) to make it a one item list: [ca_rows[row][-2]]. You need to keep those outer brackets around the str call: [str(ca_rows[row][-2])].


RE: How can I copy specif cell from one csv to another csv - zoe1111 - Jan-13-2019

Thanks. I changed to [str(ca_rows[row][-2])], but my output is still the same. Any idea? Thanks
My output:
Output:
1 blank blank blank o blank blank blank
Ideal output:
Output:
1 blank blank blank p_o blank blank blank



RE: How can I copy specif cell from one csv to another csv - ichabod801 - Jan-13-2019

(Jan-13-2019, 10:43 PM)zoe1111 Wrote: Any idea?

Not particularly. I would check the row before you output it to make sure it is being read in right. I would also check the csv file to make sure it is what you think it is.