I need some help to replace some strings/characters into a file. Because in my Django DB i have either integers or strings formats i have to customize first the CSV before doing the import.
Therefore i need in entire columns 8 and 9 the character -0- to be replaced with 0. In the rest of the file i need -0-to be replaced with "null". The name of the file is sdn.csv and is located to my Desktop C:\Users\icohen\Desktop\
Number Name B/I Program More Info Vessel CallSign Vessel Type Vessel DWT (Deadweight tonnage) Gross Registered Tonnage Vessel Flag Vessel Owner DOB/AKA
36 AEROCARIBBEAN AIRLINES -0- CUBA -0- -0- -0- -0- -0- -0- -0- -0-
173 ANGLO-CARIBBEAN CO., LTD. -0- CUBA -0- -0- -0- -0- -0- -0- -0- -0-
This is the code i used:
import csv
file = '/Users/cohen/Desktop/sdn-2.csv'
newstring = "null"
newinteger = 0
with open(file, 'r+') as f:
for row in csv.reader(f):
if row[7] =="-0-":
row[7] = newinteger
if row[8] == "-0-":
row[8] = newinteger
f.close()
Thank you in advance!