更改文件中的数字,不能使用.split

时间:2016-11-17 05:30:29

标签: python string file loops io

def process_body(infile, outfile, modification):
    '''
    changing the numbers to the outfile              
    '''


for line in infile.readline():
    line = line.strip()
    input()
    num = ""
    for char in line: 
        if modification == "negate": 
            if char != " ":
                #concatenate "digits"
                num += char
            else:
                #convert already concatenated "digits"
                negate_line = negate(num)
                print(negate_line)
                #clear for next number
                num = ""

这是我的否定函数

def negate(num):
    '''
    absolute value of RGB value - 255
    '''

    num = int(num)
    negate_line = abs(num - 255)
    negate_line_out = str(negate_line)
    return negate_line_out

这是应该从文件读取数字并减去255并打印到outfile的代码。我不被允许使用split。这是来自infile的一些代码

0 44 89 0 44 89 0 44 89 0 44 89 1 45 90 
1 45 90 1 45 90 1 45 90 1 45 92 1 45 92
1 55 101 0 54 100 0 54 100 0 53 99 0 53 99 
0 54 101 0 54 101 0 54 101 0 54 101 0 54 101 
0 54 101 0 54 101 0 54 101 0 53 103 0 53 103 

现在我的错误是 这些是我要归还的数字

255  251  246  255  251  246  255  251  246  255  251  246  254  250  

但这些是我需要的数字

255 211 166 255 211 166 255 211 166 255 211 166 254 210 165

数字处理错误是否有原因?

2 个答案:

答案 0 :(得分:1)

charnegate

时,您必须在char不同的变量中保留(连接)数字/数字,并使用" "
data = '''0 44 89 0 44 89 0 44 89 0 44 89 1 45 90 
1 45 90 1 45 90 1 45 90 1 45 92 1 45 92
1 55 101 0 54 100 0 54 100 0 53 99 0 53 99 
0 54 101 0 54 101 0 54 101 0 54 101 0 54 101 
0 54 101 0 54 101 0 54 101 0 53 103 0 53 103 '''

modification = "negate"

#for line in infile:
for line in data.split('\n'):
    line = line.strip()

    # to keep concatenated "digits"
    num = ''

    for char in line: 
        if char != " ":
            # concatenate "digits"
            num += char
        else:
            if modification == "negate": 
                # convert already concatenated "digits"
                negate_line = negate(num)
                print(negate_line, end=" ")
                # clear for next number
                num = ''

    # last number - because there is no " " after this number
    if num:
        if modification == "negate": 
            negate_line = negate(num)
            print(negate_line, end=' ')
            # clear for next number
            num = ''

    print()

def negate(num):
    num = int(num)
    negate_line = abs(num - 255)
    negate_line_out = str(negate_line)
    return negate_line_out

答案 1 :(得分:1)

不能使用拆分?没问题!写下你自己的分裂:

import itertools

def splitNums(line, delim=' '):
    answer = []
    for k,group in itertools.groupby(line, lambda x: x==delim):
        if k: continue
        g = ''.join(group).strip()
        if not g.strip(): continue
        answer.append(int(g))

    return answer


def negateNumber(n):
    return abs(n-255)

def negateFile(infilepath, outfilepath):
    with open(infilepath) as infile, open(outfilepath, 'w') as outfile:
        for line in infile:
            nums = splitNums(line)
            negs = [negateNumber(n) for n in nums]
            outfile.write(' '.join([str(i) for i in negs]))
            outfile.write('\n')

使用您的输入文件,我得到此输出文件:

255 211 166 255 211 166 255 211 166 255 211 166 254 210 165 
254 210 165 254 210 165 254 210 165 254 210 163 254 210 163 
254 200 154 255 201 155 255 201 155 255 202 156 255 202 156 
255 201 154 255 201 154 255 201 154 255 201 154 255 201 154 
255 201 154 255 201 154 255 201 154 255 202 152 255 202 152