简体   繁体   English

替换文件 python 中每一行的第一个字符

[英]replace first character of each line in file python

I am trying to replace some of the first characters in lines from multiple txt files and have this code but the output is wrong:我正在尝试替换多个 txt 文件中的一些第一个字符并具有此代码,但 output 是错误的:

for filename in glob.glob('./labels/**/*.txt', recursive=True):
with open(filename, 'r') as f:
    original_lines = f.readlines()
with open(filename, 'w') as out:
    for line in original_lines:
        if line.startswith('0'):
            line0 = line
            out.write(line0)
        if line.startswith('1'):
            line1 = line
            out.write(line1)
        if line.startswith('2'):
            line2 = line
            out.write(line2)
        if line.startswith('3'):
            line3 = line
            out.write(line3)
        if line.startswith('5'):
            line4 = '4' + line[1:]
            out.write(line4)
        if line.startswith('7'):
            line5 = '5' + line[1:]
            out.write(line5)
        if line.startswith('9'):
            line6 = '6' + line[1:]
            out.write(line6)
        if line.startswith('10'):
            line7 = '7' + line[1:]
            out.write(line7)
        if line.startswith('11'):
            line8 = '8' + line[1:]
            out.write(line8)
        if line.startswith('12'):
            line9 = '9' + line[1:]
            out.write(line9)

So if I have a file like this:因此,如果我有这样的文件:

0 0.2 0.4 0.8 0 0.2 0.4 0.8

12 0.1 0.1 0.25 12 0.1 0.1 0.25

7 0.66 0.80 0.91 7 0.66 0.80 0.91

I want output to be:我希望 output 是:

0 0.2 0.4 0.8 0 0.2 0.4 0.8

9 0.1 0.1 0.25 9 0.1 0.1 0.25

5 0.66 0.80 0.91 5 0.66 0.80 0.91

I think you have some slicing issues for example lets say your input is 12 0.1 0.1 0.25 line[1:] is going to be 2 0.1 0.1 0.25 because your input is a string.我认为您有一些切片问题,例如假设您的输入是12 0.1 0.1 0.25 line[1:]将是2 0.1 0.1 0.25因为您的输入是字符串。 You can use something like:你可以使用类似的东西:

line = '12 0.1 0.1 0.25'.split(' ') #convert your string to a list

temp = int(line[0]) #get first element and convert to integer to make comparisons easier

if temp < 3:
    print(' '.join(line))
elif temp == 5: 
    line[0] = '4'
    print(' '.join(line))
elif temp == 7:
    line[0] = '5'
    print(' '.join(line))
elif temp > 8:
    line[0] =str(temp - 3)
    print(' '.join(line))

#Output: 

9 0.1 0.1 0.25

Note: It's better to use elif instead of if in your case because if one of your conditions are true it is not going to check rest of the conditions.注意:最好使用elif而不是if在您的情况下,因为如果您的条件之一为真,则不会检查条件的 rest。 More info here更多信息在这里

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

相关问题 如何通过不触摸 python 中文本文件的每一行中该单词的 rest 来替换每行中的第一个特定特殊字符 - How to replace the first specific special character in each line by not touching the rest of that word in each of the line of a text file in python 如何替换python中“.txt”文件每一行的第一个数字? - How to replace first number from each line of a ".txt" file in python? 用Python替换文本文件行中的单个字符 - Replace Single Character in a line of a Text file with Python python用“”替换第一个字符 - python replace first character with “” 文件每一行旁边的Python字符数 - Python character count beside each line of a file 如何检测在 Python 中打开的文件的每一行中的第一个非空白字符? - How can I detect the first non-whitespace character in each line of a file opened in Python? 仅替换python中文本文件的第一行 - Replace only first line of text file in python 如何用python列表中的相应元素替换文件中每行的第一个单词? - How to replace first word of each line in a file with the corresponding element from a list in python? 用它后面的第一个更大的字符替换每个字符 - replace each character with the first greater character after it 处理文件时忽略每行的第一个字符 - First character on each line ignored while processing file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM