简体   繁体   English

在行首添加特定字符串的字符 - Python

[英]Add character at the beginning of line with specific string in line - Python

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".我想在包含字符串“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.总结:我需要编写代码,在 txt 文件中将“//”添加到包含“CC”的每一行的开头,并将其另存为新文件。

This solution works well, what you think about it ?这个解决方案效果很好,你怎么看?

filepath = input("original file :")
filepath2 = input("result file : ")

with open(filepath, "r") as f, open(filepath2, "w") as f2:
    for line in f:
        f2.write(line if not 'CC' in line else "//" + line)

It seems like a character issue in your input file.这似乎是您输入文件中的字符问题。 Maybe .strip() instead of .rstrip() will work better.也许.strip()而不是.rstrip()会更好。 .rstrip() only removes whitespaces at the right of the string while .strip() removes them left and right. .rstrip()仅删除字符串右侧的空格,而.strip()则将它们左右删除。 Something like this should work:这样的事情应该工作:

inputFile = open('data.txt', 'r')
outputFile = open('outputFile.txt', 'w')

for line in inputFile:

    outputLine = line.strip() + '\n'
    if 'CC' in line:
        outputLine = '//' +  outputLine
    outputFile.write(outputLine)
 
inputFile.close()
outputFile.close()

The extra new lines is due to the '\\n' characters present already in lines of the file, you can prevent that by changing to额外的新行是由于文件行中已经存在 '\\n' 字符,您可以通过更改为

print(line, end='')

I dont know why you prefer file input module for reading files, becuase I find the default method open to be quite satisfactory as you can read and write textfiles, binaryfiles, etc... As for your question:我不知道你为什么更喜欢文件输入模块来读取文件,因为我发现默认方法 open 非常令人满意,因为你可以读写文本文件、二进制文件等...... 至于你的问题:

with open(file_name, 'w') as file:
    file.write(data)

here is a solution :这是一个解决方案:

p="name_of_original_file.txt"
file=open(p,"r")
s=file.read()
file.close() 

new_s=""
for line in s.splitlines():
    if 'CC' not in line:
        new_s+=line+"\n"
    if 'CC' in line:
        new_s+='// '+line+"\n"
    print (line)

p="name_of_new_file.txt"
file=open(p,"w")
file.write(new_s)
file.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM