473,699 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CSV read, edit specific field, write

2 New Member
I am attempting to read a CSV file, edit a specific entry, then write back to disk. I am having trouble writing back to disk. I used split to separate fields and change. I can print the changes, but now I cannot write them back to the file. Can someone show me a quick simple example? (for simplicity I am using a small data file with 10 entries, seperated with commas, no headers)

Thank you
Aug 19 '12 #1
3 2826
bvdet
2,851 Recognized Expert Moderator Specialist
Let's say you read the csv file into a list of lists named dataList and you want to change the last element in the first line.
Expand|Select|Wrap|Line Numbers
  1. import csv
  2.  
  3. dataList = [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0],\
  4.            [0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 30.0, 30.0, 30.0, 30.0], \
  5.            [0.0, 0.0, 0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 30.0, 30.0],\
  6.            [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.0, 10.0, 15.0, 20.0, 25.0]
  7.  
  8. dataList[0][-1] = "XXXX"
  9.  
  10. fn = r'D:\tmp\some.csv'
  11. f = open(fn, 'wb')
  12. w = csv.writer(f)
  13.  
  14. w.writerows(dataList)
  15. f.close()
Aug 19 '12 #2
cswarwick
2 New Member
Ok good. How would I allow user input to choose which entry?
Aug 19 '12 #3
bvdet
2,851 Recognized Expert Moderator Specialist
That depends on what you want. You could prompt the user using built-in function raw_input (input in Python 3.x) for the line and column to change and the new value, or you could build a GUI with Tkinter or other GUI toolkit with rows and columns of widgets where the user could edit whatever he wants.
Aug 19 '12 #4

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

Similar topics

7
8758
by: bass-man | last post by:
I am trying to include a dynamically changeable file in my website with PHP and Javascript so I am using a hidden field to hold the name of the file I want to include, and I am using the$_GET function to read the hidden field, but it says the field has no value even though I have a default value in the field. So my question is how do I get PHP to read the value of a hidden field?
4
5497
by: Chuck | last post by:
I'm attempting to build a module that will allow me to update one field of a record (using a value from a previous record) based on the value of another field in the same record. I can successfully "cycle" through the records one at a time and get to the appropriate field as well, but any attempt to read the field values (using SELECT, of DoCmd.RunSQL "SELECT....) result in errors. I've searched the help file and this newsgroup but...
0
1395
by: kkrizl | last post by:
I'm having a very bizarre problem. I open up my table in datasheet view, and I click on edit, find. In the Find and Replace dialog box, I enter an address (123 main for example). If I set the Look In parameter to a specific field (Mailing Address), my record is found. If I go back up to the top of the table, and set the Look In parameter to my table name to search all of the fields in the table so that I can also search the Street...
2
1518
by: Clemens Hepper | last post by:
Hello, for my project confux (http://confux.sourceforge.net) I want to read a line from stdin. But I don't want the user to type a new line. I want so display a value which the user can edit. For example I want to ask the user for a hostname and I print "localhost", the user modified it to "localserver" and I read it after <return>.
8
6393
by: sara | last post by:
I have a table where a few of the users entered vendor names ALL IN UPPER CASE. I have created forms to edit the data, but I can't seem to allow changing JOE SMITH to Joe Smith. What to I have to do to have the user change the name, but keep the key? Overall, I think any "edits" simply add a new record, rather than change the existing record. The form is simple - Choose the vendor from the drop-down list. After Update, a new field...
3
3463
by: mckbill | last post by:
Is there a way I can direct the cursor to a specific field (variable) in a form by typing the field name while in form view? I have a form with many fields, and it would be nice if there were some method, similar to FIND RECORD (e.g., CTL + F ), where a dialog box would pop up and let me enter the field name, and then jump the cursor to that location. I currently do my edits by locating a record to be changed by ID using CTL+F and...
11
11372
by: jo3c | last post by:
hi everybody im a newbie in python i need to read line 4 from a header file using linecache will crash my computer due to memory loading, because i am working on 2000 files each is 8mb fileinput don't load the file into memory first how do i use fileinput module to read a specific line from a file? for line in fileinput.Fileinput('sample.txt')
5
4078
by: theine | last post by:
How do I create Hyperlink based of a specific field in the same row? In Excel it done like this: =HYPERLINK("", A273) where the anything inside the " " is the value it's looking for in that particular cell. How do I do a similar thing in Access?
1
3120
by: sukatoa | last post by:
I still do the process of incrementing a specific field in a stand alone apps by query 1: get its value query 2: update value +1 But, i realize that this is really a bad idea when this would be used in web applications... specially the database is in another webhost....(very slow response) i tried (using php function)
3
3544
by: Ron Mowry | last post by:
I am wanting to create a macro that will fill a specific field on a form and then go to the next record set. I have a blank field in the table that the form is pulling from. I want the user to be able to click a button I have inserted into the form which will go to the next record but I also want that button to fill in that blank field on the form with the users initials before moving to the next record. I do not want the user to type anything...
0
8623
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9196
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8941
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8896
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7784
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4390
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.