[英]Python- applying arithmetic to numbers in a text file without split
我正在处理文本文件中的数字,并从数字中减去255,但保持绝对值。
def negate(char):
'''
absolute value of RGB value - 255
'''
line = int(char)
negate_line = line - 255
negate_line = abs(negate_line)
negate_line = str(negate_line)
return negate_line
`def process_body(infile, outfile, modification):`
'''
'''
for line in infile.readlines():
line = line.strip()
for char in line:
print(char, end="")
# for debugging purposes. press enter to print next character in line
input()
if modification == "apply":
negate(line)
outfile.write(negate)
我知道我应该对字符串方法进行一些操作以自行获取数字,但是我一直无法找到一种无需使用.split
即可分隔数字的方法。 唯一的字符串方法是.strip
一旦我找出一个循环来分隔文本文件中的数字,negate函数中的char应该是返回的值,但是我目前没有值,因为我还没有弄清楚循环。
文本文件看起来像
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
def negate(num):
'''
absolute value of RGB value - 255
'''
line = int(num)
negate_line = line - 255
negate_line = abs(negate_line)
negate_line = str(negate_line)
return negate_line
和
def process_body(infile, outfile, modification):
'''
'''
num = ""
space = " "
for line in infile.readlines():
line = line.strip()
s = line + space
for char in line:
print(char, end="")
if char == space :
num += s[num +1]
# for debugging purposes. press enter to print next character in line
input()
print(num, end="")
# for debugging purposes. press enter to print next character in line
input()
if modification == "negate":
negate_line = negate(num)
outfile.write(negate_line)
if modification == "high contrast":
hc = high_contrast(num)
outfile.write(hc)
我当前的输出是0
0 4 04 4 044
044 8 0448 9 04489
但是我需要得到这样的数字
255211166255211
所以这不是一个答案,但是我不确定出什么问题了
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.