Answer a question

Below are the codes I have tried to read the text in the text file in a method called check_keyword()

def check_keyword():
    with open(unknown.txt, "r") as text_file:
        unknown = text_file.readlines()

    return unknown

This is how i called the method:

dataanalysis.category_analysis.check_keyword()

The text in the text file:

Hello this is a new text file 

There is no output for the method above :((

Answers

text_file.readlines() returns a list of strings containing the lines in the file. If you want only a string, not a list of the lines, use text_file.read() instead.

You also have another problem in your code, you are trying to open unknown.txt, but you should be trying to open 'unknown.txt' (a string with the file name).

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐