Python 为什么不打印单个单词?

Python 为什么不打印单个单词?,python,Python,我正在写一个小程序,在我的程序中有一个函数,它读取文本文件并返回句子中的单个单词。然而,即使我返回,我也很难看到打印的单个单词。我真的不明白为什么,除非我的空格有大问题。你能帮忙吗?供你参考,我只是个初学者。程序要求用户输入文件名,然后在函数中读取文件,然后将fie转换为列表,从列表中查找单个单词并将其存储在该列表中 file_input = input("enter a filename to read: ") #unique_words = [] def file(user): u

我正在写一个小程序,在我的程序中有一个函数,它读取文本文件并返回句子中的单个单词。然而,即使我返回,我也很难看到打印的单个单词。我真的不明白为什么,除非我的空格有大问题。你能帮忙吗?供你参考,我只是个初学者。程序要求用户输入文件名,然后在函数中读取文件,然后将fie转换为列表,从列表中查找单个单词并将其存储在该列表中

file_input = input("enter a filename to read: ")
#unique_words = []
def file(user): 
    unique_words = []
    csv_file = open(user + ".txt","w")
    main_file = csv_file.readlines()
    csv_file.close()


    for i in main_list:
            if i not in unique_words:
                    unique_words.append(i)


    return unique_words

#display the results of the file being read in

print (file(file_input))
对不起,我正在使用记事本:

check to see if checking works

似乎文件中每行只有一个单词

def read_file(user): 
    with open(user + ".txt","r") as f:
        data = [ line.strip() for line in f.readlines() ]
    return list( set(data) )
--更新--- 如果每行中有多个单词,并用空格分隔

def read_file(user): 
        with open(user + ".txt","r") as f:
            data = [ item.strip() for line in f.readlines() for item in line.split(' ')]
        return list( set(data) )

似乎文件中每行只有一个单词

def read_file(user): 
    with open(user + ".txt","r") as f:
        data = [ line.strip() for line in f.readlines() ]
    return list( set(data) )
--更新--- 如果每行中有多个单词,并用空格分隔

def read_file(user): 
        with open(user + ".txt","r") as f:
            data = [ item.strip() for line in f.readlines() for item in line.split(' ')]
        return list( set(data) )

如果你想要的只是文本中出现的每个单词的列表,那么你做的工作就太多了。你想要这样的东西:

unique_words = []
all_words = []
with open(file_name, 'r') as in_file:
  text_lines = in_file.readlines() # Read in all line from the file as a list.
for line in text_lines:
  all_words.extend(line.split()) # iterate through the list of lines, extending the list of all words to include the words in this line.
unique_words = list(set(all_words)) # reduce the list of all words to unique words.

如果你想要的只是文本中出现的每个单词的列表,那么你做的工作就太多了。你想要这样的东西:

unique_words = []
all_words = []
with open(file_name, 'r') as in_file:
  text_lines = in_file.readlines() # Read in all line from the file as a list.
for line in text_lines:
  all_words.extend(line.split()) # iterate through the list of lines, extending the list of all words to include the words in this line.
unique_words = list(set(all_words)) # reduce the list of all words to unique words.

您可以使用
集合
简化代码,因为它只包含唯一的元素

user_file = raw_input("enter a filename to read: ")

#function to read any file
def read_file(user):
    unique_words = set()
    csv_file = open(user + ".txt","r")
    main_file = csv_file.readlines()
    csv_file.close()

    for line in main_file:
        line = line.split(',')
        unique_words.update([x.strip() for x in line])

    return list(unique_words)

#display the results of the file being read in
print (read_file(user_file))
包含以下内容的文件的输出:

Hello, world1
Hello, world2


您可以使用
集合
简化代码,因为它只包含唯一的元素

user_file = raw_input("enter a filename to read: ")

#function to read any file
def read_file(user):
    unique_words = set()
    csv_file = open(user + ".txt","r")
    main_file = csv_file.readlines()
    csv_file.close()

    for line in main_file:
        line = line.split(',')
        unique_words.update([x.strip() for x in line])

    return list(unique_words)

#display the results of the file being read in
print (read_file(user_file))
包含以下内容的文件的输出:

Hello, world1
Hello, world2


事实上,我无法重现你的问题。提供正确的CSV输入文件1),例如

您的程序将打印此内容,这与上一个
看起来不错:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', '']
但是,您可以大大简化代码

  • 不要在每一行中附加一个
    ,然后通过
    连接,只需通过
    连接即可(这也将消除最后的
  • 使用生成器表达式,直接在
    连接中执行
    剥离

    main_string = ",".join(line.strip() for line in main_file)
    
  • 不要先加入
    join
    ,然后再拆分
    split
    ,而是使用双精度循环来理解循环列表:

    main_list = [word for line in csv_file for word in line.strip().split(",")]
    
  • 使用以下模块,而不是手动完成所有这些操作:

  • 假设顺序不重要,使用
    集合
    删除重复项:

    unique_words = set(main_list)
    
  • 如果顺序很重要,您可以(ab)使用:

  • 使用
    打开和关闭文件

总而言之:

import csv
def read_file(user): 
    with open(user + ".txt") as csv_file:
        main_list = [word for row in csv.reader(csv_file) for word in row]
        unique_words = set(main_list)  # or OrderedDict, see above
        return unique_words

1) 更新:编辑中显示的“示例文本…”文件无法使用的原因是该文件不是CSV文件。CSV的意思是“逗号分隔值”,但该文件中的单词是用空格分隔的,因此您必须按空格而不是逗号拆分

def read_file(user): 
    with open(user + ".txt") as text_file:
        main_list = [word for line in text_file for word in line.strip().split()]
        return set(main_list)

事实上,我无法重现你的问题。提供正确的CSV输入文件1),例如

您的程序将打印此内容,这与上一个
看起来不错:

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', '']
但是,您可以大大简化代码

  • 不要在每一行中附加一个
    ,然后通过
    连接,只需通过
    连接即可(这也将消除最后的
  • 使用生成器表达式,直接在
    连接中执行
    剥离

    main_string = ",".join(line.strip() for line in main_file)
    
  • 不要先加入
    join
    ,然后再拆分
    split
    ,而是使用双精度循环来理解循环列表:

    main_list = [word for line in csv_file for word in line.strip().split(",")]
    
  • 使用以下模块,而不是手动完成所有这些操作:

  • 假设顺序不重要,使用
    集合
    删除重复项:

    unique_words = set(main_list)
    
  • 如果顺序很重要,您可以(ab)使用:

  • 使用
    打开和关闭文件

总而言之:

import csv
def read_file(user): 
    with open(user + ".txt") as csv_file:
        main_list = [word for row in csv.reader(csv_file) for word in row]
        unique_words = set(main_list)  # or OrderedDict, see above
        return unique_words

1) 更新:编辑中显示的“示例文本…”文件无法使用的原因是该文件不是CSV文件。CSV的意思是“逗号分隔值”,但该文件中的单词是用空格分隔的,因此您必须按空格而不是逗号拆分

def read_file(user): 
    with open(user + ".txt") as text_file:
        main_list = [word for line in text_file for word in line.strip().split()]
        return set(main_list)


单个单词的定义在哪里?你是在寻找
唯一的单词吗?追加
?这可能是
唯一的单词
的作用,但OP应该将其移动到函数内部。@pythonslittlehalper在最后一个for循环之前添加一个
打印(主列表)
,以确保它按预期填充。为什么追加
,加入,然后按
拆分,
?只需将文件直接转换为列表!另外,只需执行
unique\u words=set(main\u list)
即可,也无法复制。使用带有几行逗号分隔值的测试文件,程序运行正常。
单个单词的定义在哪里?你是在寻找
唯一的单词吗?追加
?这可能是
唯一的单词
的作用,但OP应该将其移动到函数内部。@pythonslittlehalper在最后一个for循环之前添加一个
打印(主列表)
,以确保它按预期填充。为什么追加
,加入,然后按
拆分,
?只需将文件直接转换为列表!另外,只需执行
unique\u words=set(main\u list)
即可,也无法复制。使用带有几行逗号分隔值的测试文件,程序运行正常。我会返回
集合
列表
,因为这是OP想要的。@Ev.Kounis我会返回
集合
,因为
列表
意味着某种顺序,
集合
将销毁的。仍然不会打印sentence@pythonslittlehelper我认为问题在于你的txt的结构。你能发布一些示例吗?实际上,你需要
split()
而不是
strip()
我会返回
集合
列表,因为这是OP想要的。@Ev.Kounis我会返回一个
集合
,因为
列表
意味着某种顺序,而
集合