Python Forum
copy one column from csv file and paste into xls file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
copy one column from csv file and paste into xls file
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding the median of a column in a huge CSV file markagregory 5 1,732 Jan-24-2023, 04:22 PM
Last Post: DeaD_EyE
  Export dataframe to xlsx - Error "zipfile.BadZipFile: File is not a zip file" Baggio 10 61,378 Mar-12-2021, 01:02 PM
Last Post: buran
  Filter data based on a value from another dataframe column and create a file using lo pawanmtm 1 4,242 Jul-15-2020, 06:20 PM
Last Post: pawanmtm
  XLSX file with multiple sheets to josn file ovidius 2 2,189 Apr-05-2020, 09:22 AM
Last Post: ovidius
  How to print a column name in csv file Truman 1 4,366 Mar-31-2020, 03:34 AM
Last Post: Larz60+
  How to copy a .csv worksheet into a .xlsx file without the number values turning into YoshikageKira 7 3,465 Mar-28-2020, 10:38 AM
Last Post: buran
  [split] Converting excel file to txt file unexceptionalhobby 2 4,308 Oct-16-2019, 06:34 PM
Last Post: unexceptionalhobby
  Change column names from a file Nidhesh 2 2,961 Jul-08-2019, 06:00 AM
Last Post: Nidhesh
  Copy raw data in excel to another new excel file keerthiprashanth 5 3,821 Oct-20-2018, 10:13 AM
Last Post: volcano63
  Upload csv file as numbers (floating?) and extract element, row, and column bentaz 7 4,412 Mar-19-2018, 05:34 PM
Last Post: bentaz

Forum Jump:

User Panel Messages

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