byyou
Posts: 10
Joined: Tue Sep 10, 2019 4:33 am

rewrite value in specific column csv file

Mon Dec 30, 2019 6:10 am

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

User avatar
DougieLawson
Posts: 38517
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: rewrite value in specific column csv file

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.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

I'll do your homework for you for a suitable fee.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: rewrite value in specific column csv file

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.

byyou
Posts: 10
Joined: Tue Sep 10, 2019 4:33 am

Re: rewrite value in specific column csv file

Mon Dec 30, 2019 7:53 am

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)

byyou
Posts: 10
Joined: Tue Sep 10, 2019 4:33 am

Re: rewrite value in specific column csv file

Mon Dec 30, 2019 7:55 am

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....

tpyo kingg
Posts: 809
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: rewrite value in specific column csv file

Mon Dec 30, 2019 8:26 am

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.

User avatar
paddyg
Posts: 2521
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: rewrite value in specific column csv file

Mon Dec 30, 2019 8:41 am

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.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

Return to “Python”