我是这个论坛的新手。编程&蟒蛇。我正在努力开发我的第一个程序,但是我不断遇到关于一个特定问题的问题。我很高兴,某种鞋底可以让我摆脱痛苦。告诉我如何正确地做我想做的事。如果你知道自己在做什么,我很确定这很简单,但此刻我很愚蠢。我不知道我在做什么: - )
示例:
我需要使用2个文件,A&乙
文件A包含文字:
This is a test
文件B包含文字:
h
t
s
i
a
我需要创建一个程序,它将从文件A中一次抓取1个字符,然后搜索文件B以查找相同的字符。一旦程序找到匹配项,我希望它打印出找到匹配项的行号,然后继续从文件A&中抓取另一个字符。重复此过程直到EOF。
答案 0 :(得分:2)
好的,让我们一步一步。首先,我会将文件B
读入一个非常适合快速查找的结构,因为我们将经常这样做:
chars = {}
with open("B") as lookupfile:
for number,line in enumerate(lookupfile):
chars[line.strip()] = number
现在我们有一个字典chars
,其中包含字母作为键,行号包含值:
>>> chars
{'t': 1, 'a': 4, 'i': 3, 'h': 0, 's': 2}
现在我们可以迭代第一个文件。文件的标准Python迭代器每次迭代消耗一个行,而不是一个字符,所以最好简单地将整个文件读入一个字符串然后迭代它(因为对于字符串,迭代是逐个字符的):
with open("A") as textfile:
text = textfile.read()
现在我们遍历字符串并打印匹配值:
for char in text:
if char in chars:
print("Character {0} found in row {1}".format(char, chars[char]))
如果您不喜欢两次访问字典,也可以使用
for char in text:
found = chars.get(char): # returns None if char isn't a key in chars
if found:
print("Character {0} found in row {1}".format(char, found))
或使用例外:
for char in text:
try:
print("Character {0} found in row {1}".format(char, chars[char]))
except KeyError:
pass
答案 1 :(得分:0)
首先阅读文件A
并将其内容存储在变量中(使用file.read
)。
with open('A.txt') as f:
data = f.read() # now data is: "This is a test"
# now data is string that dontains all data of the file A.
# But as searching a character in a string is an O(N) operation
# so we must convert this string to a better data-structure.
# As we need the item as well as their first index so we
# should create a dict here, with character as the key and
# it's first appearance(first index) as it's value.
# Dicts provide O(1) lookup.
dic = {}
for ind, char in enumerate(data):
# store the character in dict only if it is an alphabet
# also check if it's already present is dict or not.
if char.isalpha() and char not in dic:
dic[char] = ind
#dic is {'a': 8, 'e': 11, 'i': 2, 'h': 1, 's': 3, 'T': 0, 't': 10}
现在打开文件B
并使用for循环迭代它,文件迭代器上的for循环一次返回一行。(内存有效方法)。
with open('B.txt') as f:
for char in f: #iterate one line at a time
char = char.strip() #str.strip strips off whitespaces like '\n'
if char in dic:
print dic[char] # if character is found in dict then
# print it's value, i.e index
...
1
10
3
2
8
答案 2 :(得分:0)
import os
fA = open('C:\\Desktop\\fileA.txt', 'r')
fB = open('C:\\Desktop\\fileB.txt', 'r')
fileb_content = []
for line in fB:
fileb_content.append(fB.read().split('\n'))
rA = fA.readline().split('\n')[0]
for c in list(rA):
if(c.strip()):
if(c.lower() in fileb_content[0]):
print(fileb_content[0].index(c.lower()))
在这里我测试该角色不是空的。