AttributeError: 尝试在file2中匹配file1的字符串标识符列表

2 投票
1 回答
11045 浏览
提问于 2025-04-16 01:49

这里是我目标的简要总结。我有一个数据文本文件,里面基本上是一些名字或标识符的列表。所有的名字都在一行上,用空格分开。我想把每个数据放在单独的一行。这些数据是标识符。如果原始数据文本文件中的某个名字也出现在一个大文件里,我希望能把那一行的数据写入一个较小的数据文件里,也就是说,把名字和一些额外的信息都放在同一行。

这是我开始尝试实现这个目标的程序。也许这对我的技能来说有点挑战,但我希望能完成它。

datafile = open ('C:\\datatext.txt', 'r')

line = [item for item in open('C:\\datatext.txt', 'r').read().split(' ') 
                  if item.startswith("name") or item.startswith("name2")]

line_list = line.split(" ")

completedataset = open('C:\\bigfile.txt', 'r')
smallerdataset = open('C:\\smallerdataset.txt', 'w')

trials = [ line_list ]


for line in completedataset:
    for t in trials: 
       if t in line:
           smallerdataset.write(line)

completedataset.close()
smallerdataset.close()

这是我在运行这个程序时在Python中遇到的错误:

Traceback (most recent call last):
  File "C:/program3.py", line 7, in <module>
    line_list = line.split(" ")
AttributeError: 'list' object has no attribute 'split'

我尽量做到非常细致,期待你们的评论。如果你们有其他问题,我会及时详细说明。祝一切顺利,享受这雨天。

编辑:

根据建议,我对程序做了一些修改。现在我的程序是这样的:

with open('C:\\datatext.txt', 'r') as datafile:
  lines = datafile.read().split(' ')
matchedLines = [item for item in lines if item.startswith("name1") or item.startswith("othername")]


completedataset = open('C:\\bigfile.txt', 'r')
smallerdataset = open('C:\\smallerdataset.txt', 'w')

trials = [ matchedLines ]


for line in completedataset:
    for t in trials: 
       if t in line:
           smallerdataset.write(line)

completedataset.close()
smallerdataset.close()

现在我遇到这个错误:

Traceback (most recent call last):
  File "C:/program5.py", line 17, in 
    if t in line:
TypeError: 'in ' requires string as left operand, not list
>>> 

感谢你们在这个问题上的持续帮助。

编辑 2:

我做了几次修改,现在我遇到了这个错误:

Traceback (most recent call last):
  File "C:/program6.py", line 9, in 
    open('C:\\smallerdataset.txt', 'w')) as (completedataset, smallerdataset):
AttributeError: 'tuple' object has no attribute '__exit__'

这是我目前的程序:

with open('C:\\datatext.txt', 'r') as datafile:
  lines = datafile.read().split(' ')
matchedLines = [item for item in lines if item.startswith("nam1") or item.startswith("ndname")]


with (open('C:\\bigfile.txt', 'r'),
      open('C:\\smallerdataset.txt', 'w')) as (completedataset, smallerdataset):
  for line in completedataset:
    for t in matchedLines:
      if t in line:
        smallerdataset.write(line)

completedataset.close()
smallerdataset.close()

我该如何克服这个难题呢?

1 个回答

2
line = [item for item in open('C:\chiptext.txt', 'r').read().split(' ')
          if item.startswith("SNP") or item.startswith("AFFY")]

这段代码把一行变成了一个字符串列表。列表对象是没有分割方法的。

看起来你想要的是从datatext中提取所有名字的列表,并且从中筛选出符合某些条件的名字。最好的做法是这样做。

with open('C:\\datatext.txt', 'r') as datafile:
  lines = datafile.read().split(' ')
matchedLines = [item for item in lines if (PREDICATE)]

总的来说,尽量不要把代码写得太复杂,尤其是一行代码搞定的那种。你的列表推导式让文件对象保持打开状态。

补充说明:matchedLines已经是一个列表了,所以我不明白你为什么在创建trials时又把它包裹在另一个列表里。下面是你正在做的事情的一个简单示例。

l = [1,2,3]
ll = [l]
print ll //[[1, 2, 3]]

当你遇到错误,而这些错误和你预期的变量值不符时,你应该加一些打印语句,这样可以确认这些值是否正确。

这可能是你需要的:

with open('C:\datatext.txt', 'r') as datafile:
  lines = datafile.read().split(' ')
matchedLines = [item for item in lines if item.startswith("name1") or item.startswith("othername")]

with open('C:\bigfile.txt', 'r') as completedataset:
  with open('C:\smallerdataset.txt', 'w') as smallerdataset:
    for line in completedataset:
      for t in matchedLines:
        if t in line:
          smallerdataset.write(line)

撰写回答