I have a file with few thousand lines like this:
0.5 AA
2.7 AA
45.2 AA
567.1 CC
667.5 AA
4456 AA
1005.2 CC
I want add comment signs "//" at the beginning of each line contains string "CC".
I have code like this:
import fileinput
file_name = input("file path: ")
for line in fileinput.FileInput(file_name, inplace=1):
if 'CC' in line:
line = line.rstrip()
line = line.replace(line,'// '+line)
print (line)
Everything works fine but the file looks like that after execute the code:
0.5 AA
2.7 AA
45.2 AA
// 567.1 CC
667.5 AA
4456 AA
// 1005.2 CC
Why after execute the code i have the new line spaces after line without changes? How I can remove this? Second question is: How i can save this file as a new file?
Summarizing: I need to write code which in a txt file will add "//" to the beginning of each line containing "CC" and save it as a new file.
\n. You need torstrip()every line.