473,657 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

csv file how to modify the row

Hi everybody
Am trying to read a csv file "temp.csv", which has the below info,
compName macAddr ipAddr
opSys
Chris-Dev 0003469F44CC 10.160.24.226 Microsoft
Windows XP
Professional
Shivayogi-Dev 000D5234F44C 10.160.24.136 Microsoft Windows XP
Professional
John-test 000D123F44CC 10.160.24.216 Microsoft
Windows XP
Professional Steve-Dev 000D123F55CC 10.160.24.116
Microsoft
Windows XP Professional
am trying to read it in the following way--
import csv
infoFile = open ('c:\\temp.csv' ,'r')
rdr = csv.reader(info File)
for row in rdr:

.... if row[0] == infnList[0]:
.... if not row[1] == infnList[1] or not row[2] ==
infnList[2]:

now am comparing my comp Name with the "compName" fields, and if it
matches i ll then compare for the "mac address" and "ip address". if
they are not matching with my system, i have to modify them there
itself, i mean i have to update "mac address" and "ip address" in the
same row itself, i dont want to append the information with another row

to the "temp.csv" file. otherwise it will have two similar computer
names for two rows, which i dont want to happen.
please give me the ways how i can work on this.
thanks in advance
yogi

Dec 27 '05 #1
4 6198
On Tue, 27 Dec 2005 14:45:16 -0800, muttu2244 wrote:
Hi everybody
Am trying to read a csv file "temp.csv", which has the below info,
compName macAddr ipAddr
opSys
Chris-Dev 0003469F44CC 10.160.24.226 Microsoft
Windows XP Professional
[snip]

Why are you calling it a Comma Separated Values file when there are no
commas separating values in it?

am trying to read it in the following way--
import csv
infoFile = open ('c:\\temp.csv' ,'r')
rdr = csv.reader(info File)
for row in rdr:


... if row[0] == infnList[0]:
... if not row[1] == infnList[1] or not row[2] ==
infnList[2]:

Should we know what infnList is, or shall we guess?

What does your code do next?
if condition: (do what?) else: (do what?)

now am comparing my comp Name with the "compName" fields, and if it
matches i ll then compare for the "mac address" and "ip address". if
they are not matching with my system, i have to modify them there
itself, i mean i have to update "mac address" and "ip address" in the
same row itself, i dont want to append the information with another row
If I have understood what you are saying, you only need to modify one
single row in the file, the row that matches your personal computer. Why
don't you just open the data file in Notepad or Wordpad, use "Find" to
search for the row you want, and modify it by hand?

If that is not what you need to do, would you like to try explaining what
you need to do a little more carefully? Do you have to modify every line,
or just some? If the data in your temp.csv file is suspect, where are you
getting the good data from?

to the "temp.csv" file. otherwise it will have two similar computer
names for two rows, which i dont want to happen.


Why not? Chris-Dev and Kris-Dev are similar but not the same, shouldn't
they get different rows?
Some questions for you to think about:

1. Is the data file small enough to all fit into memory (less than, say,
ten megabytes)?

2. Can you follow this algorithm?

open data file for reading
read everything into a list of strings
close data file
walk the list modifying each string if it needs fixing
open data file for writing
write the list of strings
close data file
3. What about this algorithm?

open data file for reading
open temporary update file for writing
for each record in data file:
is record correct?
yes: write it unchanged to update file
no: fix the record and write it to update file
close data file
close update file
delete obsolete data file
rename update file to data file


--
Steven.

Dec 28 '05 #2
Steven D'Aprano <st***@REMOVETH IScyber.com.au> wrote:
Why are you calling it a Comma Separated Values file when there are no
commas separating values in it?


It's common usage -- the Python standard library's csv also lets you
parse files where the separator is not a comma -- you just need to have
an explicit 'delimiter' (the comma is just the default value for this);
indeed, class csv.excel_tab has an explicit delimiter set to '\t'.
Alex
Dec 28 '05 #3
On Tue, 27 Dec 2005 19:24:57 -0800, Alex Martelli wrote:
Steven D'Aprano <st***@REMOVETH IScyber.com.au> wrote:
Why are you calling it a Comma Separated Values file when there are no
commas separating values in it?


It's common usage


Well you learn something new every day. Of course I know about
tab-delimited files and what-not, but that's the first time I've come
across somebody calling one a CSV file.
--
Steven.

Dec 28 '05 #4
Steven D'Aprano <st***@REMOVETH IScyber.com.au> wrote:
On Tue, 27 Dec 2005 19:24:57 -0800, Alex Martelli wrote:
Steven D'Aprano <st***@REMOVETH IScyber.com.au> wrote:
Why are you calling it a Comma Separated Values file when there are no
commas separating values in it?


It's common usage


Well you learn something new every day. Of course I know about
tab-delimited files and what-not, but that's the first time I've come
across somebody calling one a CSV file.


I realize that calling "a csv file" something that uses a delimiter
other than comma may feel weird, but we've had csv in the standard
Python library for a while, and it's hard to think of any other wholly
generic monicker. Maybe you can think of the "c" as standing for
"character" (not sure the delimiter has to be ONE character, tho;-)...
Alex
Dec 28 '05 #5

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

Similar topics

12
4904
by: Venkat | last post by:
Greetings to All, I have the following file and i need to modify it with the contents of an array File1.txt Name Location Points Grade Venkat,Newyork,100,A Jack,LA,12,C
3
2703
by: Abhas | last post by:
> > Hi, this is Abhas, > > I had made a video library program in C++, but was facing a problem. > > After entering 12 movies, i cannot enter any more movies. > > Something gibberish comes instead. > > Can somebody please tell whats wrong?? > > This is the code : : #include<fstream.h> #include<conio.h>
3
2217
by: Uday | last post by:
Hello all, I have a XML file of 10 MB, I do not want to load this file in Memory using DOM,I know that using SAX I can read the xml file but I want to modify this XML file. I want add, delete few nodes, elements or modify the value of attribute.... from XML file based on this I need to modify attribute value in respective parent nodes.
3
13383
by: Maileen | last post by:
Hi, I've asked yesterday if someone already modify data into XML file using VB.NET. In fact, my XML file is like that. .... <DB> <DB_Loc>
2
15988
by: kelly | last post by:
Hi, I don't have a code to show you, what I need are references or algorithms so that I'm in a right track. By the way, thanks for introducing me the array or arrays. Now I can continue my script. Now I want to delete a line from a file. Line being the strings I got/saved to/from array of arrays.
3
1819
by: Tinku | last post by:
Dear C++'ers I like to know how we can modify the contents in a file ex: There is a file.txt with the following data Weather=Cold WhatIDo=SleepOntheBed
5
6516
by: rn5a | last post by:
I am using the following connection to connect to a MS-Access database: --------------------------------- set con = server.createObject("adodb.connection") path1 = server.mappath("./db/MyDB.mdb") con.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& path1 &";Persist Security Info=False"
2
1937
by: panos100m | last post by:
Hi the changes that i am making using dom (see function modify) take place only in memory untill the next reload ..but not in my xml file I am doing this for a customer and i am unable to install a server side language to do a submit , how can i save the contents of xmldoc directly to a file locally ?? I was thnking of active x , but can someone someone post some code? is that the best solution? Contents of xml file :
6
4968
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am able to
0
8385
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8303
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,...
1
8502
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
8602
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
7316
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...
1
6162
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1941
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.