Python Forum
How can I copy specif cell from one csv to another csv
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I copy specif cell from one csv to another csv
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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])].
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
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
Reply
#6
(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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to copy column of cell values from one workbook to another with openpyxl curranjohn46 3 11,080 Oct-12-2019, 10:57 PM
Last Post: curranjohn46
  using openpyxl copy the value of a cell, not the formula Pedroski55 3 30,971 Jan-07-2018, 11:44 PM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020