This will let you open, read and write to the file within a single context manager in one shot.
search_term = '112' # This is what you are looking
# for at the start of the lines.
##-------------------------------------------------------##
# Define separator
sep = '\t' # Use '\t' (TAB) or " " (SPACE)
val = 0 # This is the value you want to insert
# at the end of the target line.
##-------------------------------------------------------##
write_to_file = False # Set this to True when
# you want to write to the file.
##-------------------------------------------------------##
with open("dummy.csv","r+") as f:
content = f.readlines()
for i,line in enumerate(content):
if line.startswith(search_term):
line = line.strip() + '{}{}\n'.format(sep,val)
content[i] = line
if write_to_file:
f.writelines(content)
print(''.join(content))
111 1to be like111 1 0or111 0? And each line will have only two values? seperated byspace?First empty cell: what is the separator? Tab or Space?