[英]retrieving name from number ID
I have a code that takes data from online where items are referred to by a number ID, compared data about those items, and builds a list of item ID numbers based on some criteria. 我有一个代码,该代码从在线获取数据,在该数据中,项目由数字ID引用,比较了与这些项目有关的数据,并根据某些条件构建了项目ID号的列表。 What I'm struggling with is taking this list of numbers and turning it into a list of names.
我正在努力的是将这个数字列表转换为一个名称列表。 I have a text file with the numbers and corresponding names but am having trouble using it because it contains multi-word names and retains the \\n at the end of each line when i try to parse the file in any way with python.
我有一个带有数字和相应名称的文本文件,但是使用它时遇到了麻烦,因为当我尝试使用python以任何方式解析该文件时,它包含多个单词的名称并在每行的末尾保留\\ n。 the text file looks like this:
文本文件如下所示:
number name\n
14 apple\n
27 anjou pear\n
36 asian pear\n
7645 langsat\n
I have tried split(), as well as replacing the white space between with several difference things to no avail. 我尝试了split(),并用一些不同的东西替换了之间的空白,但无济于事。 I asked a question earlier which yielded a lot of progress but still didn't quite work.
我早些时候问了一个问题,该问题取得了很多进展,但仍然没有奏效。 The two methods that were suggested were:
建议的两种方法是:
d = dict()
f=open('file.txt', 'r')
for line in f:
number, name = line.split(None,1)
d[number] = name
this almost worked but still left me with the \\n so if I call d['14']
i get 'apple\\n'
. 这几乎可以正常工作,但是仍然让我留下\\ n,所以如果我叫
d['14']
我会得到'apple\\n'
。 The other method was: 另一种方法是:
import re
f=open('file.txt', 'r')
fr=f.read()
r=re.findall("(\w+)\s+(.+)", fr)
this seemed to have gotten rid of the \\n
at the end of every name but leaves me with the problem of having a tuple with each number-name combo being a single entry so if i were to say r[1]
i would get ('14', 'apple')
. 这似乎已经消除了每个名称末尾的
\\n
,但是让我遇到了一个问题,即每个数字名称组合都是一个条目的元组,所以如果我说r[1]
我会得到('14', 'apple')
。 I really don't want to delete each new line command by hand on all ~8400 entries... 我真的不想手动删除所有〜8400个条目上的每个换行命令...
Any recommendations on how to get the corresponding name given a number from a file like this? 关于如何从这样的文件中获取给定名称的数字的任何建议?
In your first method change the line ttn[number] = name
to ttn[number] = name[:-1]
. 在您的第一种方法中,将
ttn[number] = name
更改为ttn[number] = name[:-1]
。 This simply strips off the last character, and should remove your \\n
. 这只会去除最后一个字符,并应删除
\\n
。
names = {}
with open("id_file.txt") as inf:
header = next(inf, '') # skip header row
for line in inf:
id, name = line.split(None, 1)
names[int(id)] = name.strip()
names[27] # => 'anjou pear'
Use this to modify your first approach: 使用此方法来修改您的第一种方法:
raw_dict = dict()
cleaned_dict = dict()
Assuming you've imported file to dictionary: 假设您已将文件导入字典:
raw_dict = {14:"apple\n",27:"anjou pear\n",36 :"asian pear\n" ,7645:"langsat\n"}
for keys in raw_dict:
cleaned_dict[keys] = raw_dict[keys][:len(raw_dict[keys])-1]
So now, cleaned_dict is equal to: 所以现在,cleaned_dict等于:
{27: 'anjou pear', 36: 'asian pear', 7645: 'langsat', 14: 'apple'}
*Edited to add first sentence. *编辑添加第一句话。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.