473,414 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

How to use python to write to a specific cell in a csv file?

Hello all!

I have a list of 5 characters

Expand|Select|Wrap|Line Numbers
  1. >>> li
  2. [1,2,3,4,5]
  3.  
I want to write only certain elements of this list to specific cells in a csv file.

Right now what I am doing is this:
Expand|Select|Wrap|Line Numbers
  1. myFile= open( "FILE.csv", "wb" )
  2. wtr= csv.writer( myFile )
  3. for li in zip(li):
  4.     dRow=[li]
  5.     wtr.writerow(dRow)
  6.  
  7. myFile.close()
  8.  
However, this just writes the entire list in column 1. Instead If I want to just write element 2 of the list in column 3 and row 5, how would I do that?

Thank you,
Ron Parker

Ok, I realize this is kind of a silly question because csv files i guess do not technically have cells. However, how would I do this for excel?
Feb 3 '11 #1
3 13604
bvdet
2,851 Expert Mod 2GB
The individual positions in a CSV file actually represent cells in a spreadsheet. To interact with an Excel spreadsheet, look at the xlrd and xlwt modules. Note that xlwt is for generating spreadsheets, not for writing to existing sheets. The following interaction is similar to what the CSV module would do for simple CSV data.
Initialize the data list (a list of lists representing columns and rows)
Modify a specific element using index assignment
Format the list of lists into a string suitable for writing to a CSV file
Expand|Select|Wrap|Line Numbers
  1. >>> data = [[0,0,0,0,0] for line in range(10)]
  2. >>> data
  3. [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
  4. >>> data[2][4] = [1,2,3,4,5][2]
  5. >>> data
  6. [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 3], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
  7. >>> dataStr = "\n".join([",".join([str(num) for num in item]) for item in data])
  8. >>> print dataStr
  9. 0,0,0,0,0
  10. 0,0,0,0,0
  11. 0,0,0,0,3
  12. 0,0,0,0,0
  13. 0,0,0,0,0
  14. 0,0,0,0,0
  15. 0,0,0,0,0
  16. 0,0,0,0,0
  17. 0,0,0,0,0
  18. 0,0,0,0,0
  19. >>> 
Feb 4 '11 #2
Thank you so much! However, I still have a question regarding you code. If i now write dataStr to a csv file, all of the cells that are supposed to be blank will have a "0" inside them. Is that easily fixable?
Feb 5 '11 #3
Never mind, i think i got it. I just replaced 0 with "" when making the data variable. Thanks again!
Feb 5 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Cesar Andres Roldan Garcia | last post by:
Hi I'm trying to write an hexadecimal file... I mean not a text plain... I have to convert a float decimal number in float hexadecimal one, and that's done. That number is the one I'm gonna...
18
by: reclusive monkey | last post by:
Firstly is this possible?! I've been playing around with XML for various things, and it would be very helpful if I could manage to write to an XML file with no server-side work. The reason for...
3
by: Michael Schindler | last post by:
Hello NG How I can delete a specific cell in my Datagrid. I would like if the user is in the column 6 the value changed, after automaticly the value in the column 7 in the same row to delete....
2
by: tenzeel. | last post by:
i wants that the Event would fie when we click on DataGrid using Asp.net without using any select command or hyperlink. i just want that user click on the specific cell of datagrid then the even...
1
by: Gidi | last post by:
Hi, Is it possible to Disable editing of one specific cell in one specific row in dataGrid? Thanks, Gidi.
1
by: mikename | last post by:
Hi, I'm currently working on a project which is essentially a clickable world map. I have successfully mapped the image and using a OnMouseOver = "Continent()" OnMouseOut = "World()" command in...
2
by: dismantle | last post by:
Hi everyone im new at vb and i already need help. Is it possible to get a specif cell in a column that i have created using sql database. For example: I made a table that have a 3 columns...
1
by: tbakas2 | last post by:
Thank you in advance for the help. I'm using Microsoft XP, I'm in the VBA editor in Excel. I'm trying to write code that will assign a positive or negative value based upon what the adjacent cell...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
5
by: omar999 | last post by:
im able to select & display all table data from sql database on to a webpage. but when I use the response.write method is it possible to output a specific cell with a WHERE or LIKE condition? ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.