I'm trying to write a field on a csv file and then print it on the console, however when printing the first line after writing it, it shows a number 10 (which i'm not adding to the field. Then when i'm just reading the file and printing it the second line shows what I was supposed to write on the first attempt but showing \ufeff
Here is the code
from csv import DictReader
with open('loopbacks2.csv', mode='r+', encoding='utf-8-sig') as csv_file:
client_details = DictReader(csv_file)
for client in client_details:
client['State'] = csv_file.write('Completed')
print(client)
Original csv file looks like this
Name,Loopback,Object,IP,SSH,State
Device1,192.168.1.2,host1,8.7.6.5/32,2022
Device2,192.168.1.3,host2,8.7.7.2/32,2222
When running the script, this is the output
{'Name': 'Device1', 'Loopback': '192.168.1.2', 'Object': 'host1', 'IP': '8.6.5.6/32', 'SSH': '2022', 'State': 10}
Then I open the file and it looks like this
Name,Loopback,Object,IP,SSH,State
Device1,192.168.1.2,host1,8.7.6.5/32,2022
Device2,192.168.1.3,host2,8.7.7.2/32,2222,Completed
Then if i comment the writing part and just print it like this code
from csv import DictReader
with open('loopbacks2.csv', mode='r+', encoding='utf-8-sig') as csv_file:
client_details = DictReader(csv_file)
for client in client_details:
#client['State'] = csv_file.write('Completed')
print(client)
Then the prompt shows this
{'Name': 'Device1', 'Loopback': '192.168.1.2', 'Object': 'host1', 'IP': '8.6.5.6/32', 'SSH': '2022', 'State': ''}
{'Name': 'Device1', 'Loopback': '192.168.1.2', 'Object': 'host1', 'IP': '8.6.5.6/32', 'SSH': '2022', 'State': '\ufeffCompleted'}
what am I missing or confusing?
Thanks in advance