[英]How do I do a character compare on text box data in python 3
I have looked all over and can't find what I'm looking for. 我四处张望,找不到我要找的东西。 I have recently started learning python but am now stuck on my one project. 我最近开始学习python,但现在被困在我的一个项目中。
I have a text box called self.script_txt1
and I'm trying to write a function that will compare every row in that text box with each other to find the odd character. 我有一个名为self.script_txt1
的文本框,我正在尝试编写一个函数,该函数会将文本框中的每一行相互比较以找到奇数字符。
I know I will need to use a for loop to do this but I can't figure out how to create the loop so that it will run through each char per line then move on to the next row. 我知道我将需要使用for循环来执行此操作,但我无法弄清楚如何创建循环,以便它将在每行的每个字符中运行然后移至下一行。
For Example, the data inside self.script_txt1
例如, self.script_txt1
的数据
1111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111
111111111111111111111111111111111a111111111
1111111111111111111111111111111111111111111
11111111a1111111111111111111111111111111111
1111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111
1111911111111111111111a11111111111111111111
111111111111111111Z111111111111111111111111
1111*111*111111111*111*1111111111*111111111 # <-- new inserted line
I would like to find all the characters that are different and insert a new line at the bottom with a *
or %
to mark the position where the different char was found. 我想找到所有不同的字符,并在底部用*
或%
插入新行,以标记找到不同字符的位置。
Read comments in code 阅读代码中的注释
text = '''1111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111
111111111111111111111111111111111a111111111
1111111111111111111111111111111111111111111
11111111a1111111111111111111111111111111111
1111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111
1111911111111111111111a11111111111111111111
111111111111111111Z111111111111111111111111'''
# split text into lines
lines = text.split('\n')
# find length of the longest line
length = max(len(line) for line in lines)
print('length:', length)
# create new line with `1`s (or rather create list with `1`s)
result = ['1']*length
print(' start:', ''.join(result))
# search for chars in lines
# get line-by-line
for line in lines:
# get char-by-char in line (and enumerate it)
for idx, char in enumerate(line):
# check char
if char != '1': # if char not in ('1', 'x', 'y'):
# put `*` in correct place in new line
result[idx] = '*'
#print('found:', idx, char)
# display new line
new_line = ''.join(result)
print('result:', new_line)
Result: 结果:
length: 43
start: 1111111111111111111111111111111111111111111
result: 1111*111*111111111*111*1111111111*111111111
Now you have to only get text from textbox and add new line at the end. 现在,您只需从文本框中获取文本,然后在末尾添加新行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.