Page 1 of 1

rewrite value in specific column csv file

Posted: Mon Dec 30, 2019 6:10 am
by byyou
Hi all...

I have a problem related to saving variables into a .csv file. I have a file called coordinate.csv.
in the coordinate.csv file there are variables as follows :

Row 0 Row 1
column : 0 ---> T-Coordinate S-Coordinate
column: 1---> 40 | 0 | 0 | 70 | 3 0 | 0

let say, i want to change variable in column 1 with the new one, for example : 50 | 20 | 0 | 70 | 3 0 | 0
so my coordinate.csv will be countain data as follows:

Row 0 Row 1
column : 0 ---> T-Coordinate S-Coordinate
column: 1---> 50 | 20 | 0 | 70 | 3 0 | 0


i have a code like this:

fb.write(str(50)+","+str(20)+","+str(0)+","+str(70)+","+str(30)+","+str(0))
fb.close()

but instead rewrite the exact value in column 1, it became:
Row 0 Row 1
column : 0 ---> T-Coordinate S-Coordinate
column: 1---> 40 | 0 | 0 | 70 | 3 0 | 0 |50 | 20 | 0 | 70 | 3 0 | 0 | 50 | 0 | 0 | 70 | 3 0 | 0 |50 | 20 | 0 | 70 | 3 0 | 0 ---> going inifinity

how I should i do it? please let me know if there is a certain way to do.....thanks all

Re: rewrite value in specific column csv file

Posted: Mon Dec 30, 2019 6:14 am
by DougieLawson
You'll be better off writing your data to an SQL database. That allows any column in any row (ignoring foreign keys and other constraints) to be re-written.

Once the data is in a database row extraction as CSV is a trivial SQL select.

Re: rewrite value in specific column csv file

Posted: Mon Dec 30, 2019 7:42 am
by tpyo kingg
The Python Standard Library has 'csv' which can read (and write) some CSV files in a way which is easy to work with.

However, I'd second the suggestion to consider SQL, if you have a lot of data to work with. SQLite, PostgreSQL, and MariaDB (formerly MySQL) are useful there.

Re: rewrite value in specific column csv file

Posted: Mon Dec 30, 2019 7:53 am
by byyou
tpyo kingg wrote:
Mon Dec 30, 2019 7:42 am
The Python Standard Library has 'csv' which can read (and write) some CSV files in a way which is easy to work with.

However, I'd second the suggestion to consider SQL, if you have a lot of data to work with. SQLite, PostgreSQL, and MariaDB (formerly MySQL) are useful there.
i do use csv standart library, but i dont know how to use some of its function to rewrite the csv file. i have some of experience to make data logger using csv library. When I make data logger the value is just append without deleting or replacing previous but in this case i want to replace the previous value in the same address (same row and column)

Re: rewrite value in specific column csv file

Posted: Mon Dec 30, 2019 7:55 am
by byyou
DougieLawson wrote:
Mon Dec 30, 2019 6:14 am
You'll be better off writing your data to an SQL database. That allows any column in any row (ignoring foreign keys and other constraints) to be re-written.

Once the data is in a database row extraction as CSV is a trivial SQL select.
thanks for the suggestion sir.....my intention is just to manipulate a small data (small csv file), its just like 1 or 2 row and column...so i think its too complex if i do it using SQL database....

Re: rewrite value in specific column csv file

Posted: Mon Dec 30, 2019 8:26 am
by tpyo kingg
byyou wrote:
Mon Dec 30, 2019 7:53 am
i do use csv standart library, but i dont know how to use some of its function to rewrite the csv file.
As far as I know you cannot edit the file in place using the CSV module and would have to make a copy.

Code: Select all

import csv

with open('coordinate.csv', newline='') as csvfile, \
     open('coordinate-copy.csv', 'w') as csvout:
    data = csv.reader(csvfile, delimiter='|')
    writer = csv.writer(csvout, delimiter='|')

    for row in data:
        row[0] = '50 '
        writer.writerow(row)
You might look at 'tempfile' for the intermediate file.

Re: rewrite value in specific column csv file

Posted: Mon Dec 30, 2019 8:41 am
by paddyg
You can read and write into the middle of a file but you would have to have a scheme for keeping track of the data in a structured form. That's what a db would do for you. If it's only a small amount of data read and write it into a variable using pickle or json. Or csv as suggested.