python中按索引访问单词

2024-10-05 12:35:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道这是否可能,但我正在尝试通过索引从拆分字符串中访问单词(而不是单个字符),并将其存储在字典中。如果这不能工作,请有任何其他的建议,如何着手获得相同的结果。这是我目前的代码:

def main():
if len(argv) != 2:
    print("usage: python import.py (csv file)")
    exit(0)


db = SQL("sqlite:///students.db")

file = open(argv[2], 'r')
csv_file = DictReader(file)

for row in csv_file:
    names = row['name']
    for i in names:
        word = i.split()

  # what the csv looks like
  name,house,birth
  Adelaide Murton,Slytherin,1982
  Adrian Pucey,Slytherin,1977
  Anthony Goldstein,Ravenclaw,1980
   
  # what i want it to look like
  first name,middle name,last name,house,birth
  Adelaide,None,Murton,Slytherin,1982
  Adrian,None,Pucey,Slytherin,1977
  Anthony,None,Goldstein,Ravenclaw,1980 
       

Tags: csvnameinnonefordbnameswhat
3条回答
sentence = 'This is an example'  # string: 'This is an example'
words = sentence.split()         # list of strings: ['This', 'is', 'an', 'example']

此时,您可以通过调用其索引来获取特定的单词,或者像for word in words:一样循环遍历它们

我不确定代码中的SQL部分,但在执行for i in names:时,似乎已经在循环使用这些单词了

如果单词之间有逗号,则可以执行words = i.split(',')或将分隔符作为参数传递给split()的任何操作

您可以尝试使用此代码

def create_word_index(filenames, rare_word_flag=False):
    word_index, word_count, single_words = {}, 0, []  # original : word_count = 0

    ## Handle word index and word count
    for idx, filename in enumerate(filenames):
        with open(filename) as f:
            for sentence in f.readlines():
                words = sentence.strip().split()
                for word in words:
                    # word = process(word)  # Do more preprocessing stuff here if you need
                    if rare_word_flag and (word in single_words):
                        word_index[word] = 1
                        continue
                    if word in word_index:
                        continue
                    word_index[word] = word_count
                    word_count += 1
    return word_index, word_count


filenames = ["haha.txt"]
word_idx, word_count = create_word_index(filenames, False)
print(word_idx)
print(word_count)

# haha.txt file:
name=huhu, check=ok
name=haha, check=not good

相关问题 更多 >

    热门问题