Python Forum
copy one column from csv file and paste into xls file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: copy one column from csv file and paste into xls file (/thread-13850.html)



copy one column from csv file and paste into xls file - kprogrammer - Nov-03-2018

I am a beginner in python. I ma working on csv file and xls file both.
how can i copy one column of csv fle and paste it into xls file in paste special mode.

To open csv file i got 2 options 1)by importing csv 2)by importing pandas
To open xls file i am importing xlrd.

i am not uderstanding how i can code to copy and paste using two different libraries.

looking for useful links or suggestions.please guide me.
Thanks in advance.

code :-

import xlrd
import pandas as pd
import openpyxl
import csv

# path of xls file
file_location = "C:/Users/Kanchan/Desktop/try.xls"
#path of csv file
file_location_p = "C:/Users/Kanchan/Desktop/NAVUpdate_31-Aug-2018.csv"

# working with xls file using xlrd

workbook = xlrd.open_workbook(file_location)
sheet = workbook.sheet_by_index(0)
value = sheet.cell_value(0,0)
data = pd.read_csv(file_location_p)
print(data.head() )
print(value)
data1 = [[sheet.cell_value(r,c) for c in range (sheet.ncols)] for r in range (sheet.nrows)]
print(data1[0][0])
print(sheet.ncols)
for col in range (sheet.ncols):
print(sheet.cell_value(0,col))
for col in range (sheet.ncols):
print(sheet.cell_value(2,col))
print(data1[1][1])


#using csv to work with csv file
rows = []
# reading csv file
with open(file_location_p, 'r') as csvfile:
# creating a csv reader object
csvreader = csv.reader(csvfile)

# extracting each data row one by one
for row in csvreader:
rows.append(row)

# copy xls file data using xlrd
rows =sheet.nrows
columns =sheet.ncols
print("rows=",rows)
print("columns=",columns)
listab = []
for i in range(1,rows+1):
listab.append([])

print(listab)
for r in range(1,rows+1):
for c in range(1,2):
listab[r-1].append(sheet.cell_value(r-1,c-1))

print(listab)