Hi, advanced thanks for any help...
I'm relatively new to programming (two weeks or so), so forgive me if I make some terminological errors here.
I'm using OS X and have a collection of excel files with a lot of data on them. I want to take one particular column out of a particular excel file, and put it into a new excel file. How can I do this?
5 3076 bvdet 2,851
Expert Mod 2GB
Module xlrd is a cross platform tool for reading Excel files. Example: - import xlrd
-
wb = xlrd.open_workbook('my_spreadsheet.xls')
-
-
sheetList = wb.sheet_names()
-
-
sheet1 = wb.sheet_by_index(6)
-
sheet2 = wb.sheet_by_name(u'Embeds')
-
-
for rownum in range(sheet1.nrows):
-
print sheet1.row_values(rownum)
-
-
sixth_column = sheet1.col_values(5)
-
-
cell_A1 = sheet1.cell(0,0).value
-
cell_C4 = sheet1.cell(rowx=3,colx=2).value
Read the column of data that you want and create a csv file. If you want the data in an Excel column, you will need to put each data item on a separate line. Examples: - # add column data on one row
-
f =open('sixth_column.csv', 'w')
-
f.write(",".join(sixth_column))
-
f.close()
-
-
# add column data in column
-
f = open('sixth_column1.csv', 'w')
-
f.write("\n".join(sixth_column))
-
f.close()
The CSV files can be opened in Excel.
To add another column to an existing CSV file (simplified version): - fList = [item.strip() for item in open('sixth_column1.csv', 'r').readlines()]
-
f = open('sixth_seventh_columns.csv', 'w')
-
f.write('\n'.join([','.join([item1, item2]) for item1, item2 in zip(fList, sheet1.col_values(6))]))
-
f.close()
To add another column as a row to an existing file: - f = open('sixth_column.csv', 'a')
-
f.write("\n"+",".join(sheet1.col_values(6)))
-
f.close()
You could also do this with VBA. Although I have a nasty feeling that excel for OS X doesn't have VBA. It's been a while since I played with excel, and I don't have it anymore, but it's not very hard to do.
On the other hand it looks like bvdet has shown you a pretty easy way! Nice one bvdet! Very cool
This was very helpful, so thank you very much.
After playing around with this for quite a while today, I think I'm well on my way to figuring out the specific task I have.
I can extract a particular column from an excel .xls file just fine. Let's say I extract it from extract_file.xls. I then use the command to join that column with another file - say, 'other_file.csv' - and that seems to work. But when I try to open other_file.csv in Excel, it does not have the column I thought I had added.
Here's what I've been doing specifically: -
import xlrd
-
Path = '/users/myname/Desktop/'
-
File = 'extract_file.xls'
-
wb = xlrd.open_workbook('/users/myname/Desktop/'extract_file.xls')
-
sheetList = wb.sheet_names()
-
Sheet1 = wb.sheet_by_index(0)
-
sixth_column = Sheet1.col_values(5)
-
for rownum in range(Sheet1.nrows):
-
print sixth_column
-
# With the above steps, I can extract just fine. But I run into trouble when I try to add to the other file - namely, 'other_file.csv'
-
-
Path = 'users/my_name/Desktop/'
-
File = other_file.csv'
-
f = open('other_file.csv', 'w')
-
f.write("\n".join(sixth_column))
-
f.close
# But it doesn't change the file...
bvdet 2,851
Expert Mod 2GB
Please use code tags when posting code. See posting guidelines.
If you want to add to another file, do not use write ('w') mode. The file will be truncated to 0 bytes. Use append ('a') mode. Pay close attention to my example. Also, you must add parentheses after f.close. f.close returns the function, but you want to call the function.
Great. That helps quite a bit. Thank you.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Jeremy Langworthy |
last post by:
Hi
I am trying to create a MS Excel format CSV but I can't figure out how
to get the line feed/carriage return/new record working properly.
I am nding each line/record with these characters:...
|
by: Guy Incognito |
last post by:
Hello,
I've written an asp.net application that creates Excel documents. It
works by creating an excel document in XML format.
But I wonder if I'm reinventing the wheel. I know that there are...
|
by: Paul |
last post by:
Hi all
Arggghhh...........
The problem.....I want the user to be able to create an excel document and
name particular cells in the document where they want the data to be placed
and then save...
|
by: Ange T |
last post by:
Hi there,
I'm having pain with the VB behind an Access form. The form is used to
create reports in Excel based on the details entered in the form. This
has always worked without error on my...
|
by: Oci-One Kanubi |
last post by:
Everything works fine in Access, but when I double-click on the
resultant Excel files the first one opens correctly, but subsequent
ones, and any other Excel files I try to open, fail to display at...
|
by: Ken Hall |
last post by:
Is it possible to create an Excel spreadsheet using VB.NET without
opening the Excel application or having to have the Excel application
on the operating computer?
KH
|
by: Andy Burchill |
last post by:
Hi there,
I am trying to find out what the best way of creating an excel
spreadsheet is, this will need to be done dynamically by an aspx website
on the server side.
The main way would be to...
|
by: reesa |
last post by:
Hi All,
I am having problem creating excel pivot table from asp pages.
I manage to create excel sheet with teh calculation wanted.But wondering how to make to excel pivot.I know how to create...
|
by: vijayarl |
last post by:
Hi Everyone,
Am trying to collate the individual xls files into a single xls file.i have written this code.
use strict;
use Spreadsheet::WriteExcel;
use Spreadsheet::ParseExcel;
use...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |