aniketh shet
aniketh shet

Reputation: 11

How to edit specific value of a CSV file?

I am trying to edit a specific value in a CSV file to subtract the quantity value with a specific TYRE SIZE. Here is my code:

import uuid
import pyqrcode
import csv

q = str(uuid.uuid1())
print(q)

uuidcreate = pyqrcode.create(q)

uuidcreate.png('myqr'+q+'.png', scale=6)
uu = str(input('UUID:'))
uuid = uu

with open('data.csv','a')as f:
  data = csv.reader(f)
  for row in data:
        if uu in row:
            print(row)

the csv:

TYRE SIZE,BRAND,PATTERN,TT/TL,QUANTITY,UUID
135/75R14,CEAT,MILEAGE,TL,2,23e89850-ea41-11ec-bfce-68fef70199d5

Any help would be appreciated. Thank You

Upvotes: 0

Views: 98

Answers (1)

Marcian0
Marcian0

Reputation: 21

You can aproach this with pandas, pandas has the readCsv comand that will return you a dataframe witch is very usefull for data manipulation. If you want to subtract the quantity of a specific tyre size you can make something like this:

import pandas as pd
df = pd.read_csv('your_file.csv',sep='your csv separator')
df[df['TYRE SIZE']=='Tire Size you want to subtract']['QUANTITY']-=ammount_you_want_to_subtract

Edit 1: The code would look like this:

import pandas as pd
input_uuid = input('Insert an UUID here')
remove = input('Insert amount to remove here')

df = pd.read_csv('mycsv.csv',sep=';')
df[(df['UUID'])==input_uuid]['QUANTITY'] -= int(remove)

df.to_csv('output.csv',index=False)

I would recomend you make this on a jupyter notebook, it is easy to see what is hapening to the code with it

Upvotes: 1

Related Questions