Python- 在不拆分的情况下对文本文件中的数字应用算术
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
但我需要得到这样的数字
255 211 166 255 211
所以这不是一个完整的答案,但我不确定出了什么问题
我正在研究从文本文件中获取数字并从数字中减去 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
但我需要得到这样的数字
255 211 166 255 211
所以这不是一个完整的答案,但我不确定出了什么问题