简体   繁体   English

读取文件时,Python提取单个字符串

[英]Python pull individual strings when reading a file

I am brand new to Python. 我是Python的新手。 I am working through an example on reading files and I've been looking at this for a while and can't figure out what I am doing wrong. 我正在通过一个读取文件的示例进行研究,并且已经研究了一段时间,但无法弄清楚我在做什么错。 What I am trying to do is follow the same logic as if I were searching for a string from a source file to see if it exists in a destination file. 我想做的是遵循与从源文件中搜索字符串以查看其是否存在于目标文件中相同的逻辑。 Think of it a my source file being the string dictionary and the destination file being the data file that the strings will be searched for. 考虑一下,我的源文件是字符串字典,目标文件是要搜索字符串的数据文件。 That seems to be working, but what I now want to do is pass each of the strings one at a time to a custom module called diff to compare those strings. 这似乎可行,但是我现在想要做的是一次将每个字符串传递给一个名为diff的自定义模块,以比较这些字符串。 What is happening now is that it's throwing the results off because i'm passing a list of strings instead of one at a time. 现在发生的是,由于我传递的是字符串列表,而不是一次传递一个字符串,因此结果被丢弃了。 How do I pass the strings one at a time to my diff command below. 我如何一次将字符串传递给下面的diff命令。

with open('./dic') as f:
    dictionary = f.read()

if not dictionary:
    sys.exit("Could not read dictionary data :-(")

with open('./dat') as f:
    for dataFile in (line.strip() for line in f):
        print 'dataFile: ', dataFile
        print 'dictionary: ', dictionary
        score=diff.dataL(dataFile, dictionary)
        print 'Diff score: ', score

For example my output looks like this - as you can see it's passing 3 items every time for dictionary instead of 1. 例如,我的输出看起来像这样-您可以看到它每次为字典传递3个项目,而不是1个。

dataFile:  aaaa
dictionary:  aaaa
bbbb
cccc

dataFile:  test test
dictionary:  aaaa
bbbb
cccc

dataFile:  fail fail
dictionary:  aaaa
bbbb
cccc

Thanks for the help! 谢谢您的帮助!

Your variable dictionary is actually a String, so it is technically one item. 您的可变字典实际上是一个字符串,因此从技术上讲它是一项。 If you want to make it a list, for example to iterate over, you can do this: 如果要使其成为列表,例如要进行迭代,则可以执行以下操作:

dictionary_list = dictionary.split()

This will make every string separated by whitespace in the String dictionary into an item in the list dictionary_list. 这将使“字符串”字典中由空格分隔的每个字符串成为列表dictionary_list中的一个项目。

For this line: 对于此行:

dictionary = f.read()

You want to use the readlines method that returns all lines in a list 您想使用readlines方法返回列表中的所有行

Not the read method that returns all the file content in a string 不是以字符串形式返回所有文件内容的read方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM