【问题标题】:I'm trying to find words from a text file in another text file我正在尝试从另一个文本文件中的文本文件中查找单词
【发布时间】:2023-01-23 23:42:02
【问题描述】:

我构建了一个带有篮球信息的简单图形用户界面 (GUI),以便更轻松地查找有关球员的信息。 GUI 利用使用“请求”库从各种来源抓取的数据。它运行良好,但有一个问题;在我的代码中有一个玩家列表,必须将这些玩家列表与这些抓取的数据进行比较,以便一切正常工作。这意味着如果我想在此列表中添加或删除任何名称,我必须进入我的 IDE 或直接进入我的代码 - 我需要更改它。拥有一个可以存储所有这些玩家名称的外部文本文件将在管理它们时提供非常需要的灵活性。

#This is how the players list looks in the code.
basketball = ['Adebayo, Bam', 'Allen, Jarrett', 'Antetokounmpo, Giannis' ... #and many others]

#This is how the info in the scrapped file looks like:

Charlotte Hornets,"Ball, LaMelo",Out,"Injury/Illness - Bilateral Ankle, Wrist; Soreness (L Ankle, R Wrist)"
"Hayward, Gordon",Available,Injury/Illness - Left Hamstring; Soreness
"Martin, Cody",Out,Injury/Illness - Left Knee; Soreness
"Forbes, Bryn",Questionable,Injury/Illness - N/A; Illness,
"Okogie, Josh",Questionable,Injury/Illness - Nasal; Fracture,

#The rest of the code is working well, this is the final part where it uses the list to write the players that were found it both files.

with open("freeze.csv",'r') as freeze:
    for word in basketball:
        if word in freeze:
            freeze.write(word)

# Up to this point I get the correct output, but now I need the list 'basketball' in a text file so can can iterate the same way

# I tried differents solutions but none of them work for me

with open('final_G_league.csv') as text,  open('freeze1.csv') as filter_words:
    st = set(map(str.rstrip,filter_words))
    txt = next(text).split()
    out = [word  for word in txt if word not in st]

# This one gives me the first line of the scrapped text

import csv

file1 = open("final_G_league.csv",'r')
file2 = open("freeze1.csv",'r')

data_read1= csv.reader(file1)
data_read2 = csv.reader(file2)

# convert the data to a list
data1 = [data for data in data_read1]
data2 = [data for data in data_read2]

for i in range(len(data1)):
    if data1[i] != data2[i]:
        print("Line " + str(i) + " is a mismatch.")
        print(f"{data1[i]} doesn't match {data2[i]}")

file1.close()
file2.close()

#This one returns a list with a bunch of names and a list index error.

file1 = open('final_G_league.csv','r')
file2 = open('freeze_list.txt','r')

list1 = file1.readlines()
list2 = file2.readlines()

for i in list1:
    for j in list2:
        if j in i:

# I also tried the answers in this post:
#https://stackoverflow.com/questions/31343457/filter-words-from-one-text-file-in-another-text-file

【问题讨论】:

  • 您的文件是否包含抓取信息的选择行,播放器名称始终位于行的开头,后跟该播放器的信息?

标签: python csv text-files


【解决方案1】:

如果 file2 只是一个名称列表,并且想要提取第一个文件中名称列与列表中的名称匹配的那些行,则可以执行类似的操作。

import csv

with open("freeze1.csv",'r') as file2:
  reader = csv.reader(file2)
  next(reader) # skip header
  # convert the data to a list
  names = [row[0] for row in reader]

print("names:", names)

with open("final_G_league.csv",'r') as file1:
  reader = csv.reader(file1)
  next(reader) # skip header
  for row in reader:
    if row[0] in names:
      # print matching name that matches
      print(row[0])

如果名称与 final_G_league 文件中出现的名称不完全匹配,则可能需要进行相应调整,例如进行不区分大小写的匹配或规范化名称(姓氏、名次与名次)等。

【讨论】:

    猜你喜欢
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-29
    • 2013-04-28
    • 2020-02-01
    • 1970-01-01
    相关资源
    最近更新 更多