Change content of the same column and write to the same file
I wanted to change the content of column 2(a single character) of every
line in a file into alphabet "k", here is my code:
f = open('test.txt')
f_str = f.readlines()
f.close()
for line in f_str:
s = list(line)
s[1] = "k"
line = ''.join(s)
print line
f = open('test.txt', 'w')
f.writelines(f_str)
f.close()
I managed to change the content in the for loop(the print result shows
that), but I failed to write the changed result to the file. Please, any
suggestions will be greatly appreciated.
No comments:
Post a Comment