Bottom Page

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)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [split] Converting excel file to txt file unexceptionalhobby 2 265 Oct-16-2019, 06:34 PM
Last Post: unexceptionalhobby
  Change column names from a file Nidhesh 2 371 Jul-08-2019, 06:00 AM
Last Post: Nidhesh
  How to delete column if entire column values are "nan" Sri 4 608 Apr-13-2019, 12:16 PM
Last Post: Sri
  Copy raw data in excel to another new excel file keerthiprashanth 5 734 Oct-20-2018, 10:13 AM
Last Post: volcano63
  Upload csv file as numbers (floating?) and extract element, row, and column bentaz 7 1,221 Mar-19-2018, 05:34 PM
Last Post: bentaz
  Converting excel file to txt file muhsin 3 9,378 Oct-13-2017, 07:52 PM
Last Post: nilamo

Forum Jump:


Users browsing this thread: 1 Guest(s)