473,382 Members | 1,377 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,382 software developers and data experts.

How do I read and write to the same CSV file

I want to update one field in a row for a csv file. So far my code
looks something like this

cf_stream = open("\\config.csv","r+")
csv_file = csv.DictReader(cf_stream, ['Algorithm','LastModified'])

and then I know I can do something like this

for row in csv_file
name = row["Algorithm"]
date = row["LastModified"]

now what I want is something like this

if date == 0:
date = os.getmtime()
# now this is where I want to write into the row of the same csv
file but only updating that one field

How can I do this?

Sep 10 '07 #1
3 1414
On Mon, 10 Sep 2007 07:27:46 -0700, Chris wrote:
if date == 0:
date = os.getmtime()
# now this is where I want to write into the row of the same csv
file but only updating that one field

How can I do this?
You can't. Modifying text files with variable length lines is
"impossible" or at least not worth the trouble because if the replacement
line does not have the very same length, all lines after that changed line
have to be moved, i.e. rewritten anyway.

Write the rows to another file and modify the desired entry on the fly and
rename the temporary file to the source file's name afterward.

Ciao,
Marc 'BlackJack' Rintsch
Sep 10 '07 #2
On Mon, 10 Sep 2007 07:27:46 -0700, Chris wrote:
I want to update one field in a row for a csv file. So far my code looks
something like this

cf_stream = open("\\config.csv","r+")
csv_file = csv.DictReader(cf_stream, ['Algorithm','LastModified'])

and then I know I can do something like this

for row in csv_file
name = row["Algorithm"]
date = row["LastModified"]

now what I want is something like this

if date == 0:
date = os.getmtime()
# now this is where I want to write into the row of the same csv
file but only updating that one field

How can I do this?
With difficulty unless the bytes you are writing to are exactly the same
length as the bytes that were there before.

In my opinion, unless you're writing a file with fixed-length fields, and
maybe not even then, give up the dream of writing directly in place.
Unless your file is HUGE, the chances are that trying to modify it in
place is not only harder work but also slower.

But if you insist... you might be able to use cf_stream.seek() to move to
the current position in the file, then write the bytes you want. How do
you find the current position? Black magic or voodoo might help.
Otherwise you might count every character of every line up to the field
you want. Don't forget delimiters, quote marks and newlines. Good luck.

--
Steven.
Sep 10 '07 #3
On Mon, 10 Sep 2007 15:01:32 +0000, Steven D'Aprano wrote:
But if you insist... you might be able to use cf_stream.seek() to move
to the current position in the file, then write the bytes you want. How
do you find the current position? Black magic or voodoo might help.
<slaps head>

Well, that'll learn me to post at ungodly hours of the morning. Getting
the _current_ position is easy, what I meant to write was CORRECT
position. Twice.

How embarrassment.
--
Steven.
Sep 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
5
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
5
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or...
3
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its...
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need only 1st line to read and upto 120 bytes echo...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
1
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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
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...

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.