How do I read a text file as a string?
up vote
-3
down vote
favorite
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 :((
python text-files
add a comment |
up vote
-3
down vote
favorite
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 :((
python text-files
1
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
1
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32
add a comment |
up vote
-3
down vote
favorite
up vote
-3
down vote
favorite
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 :((
python text-files
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 :((
python text-files
python text-files
edited Nov 12 at 9:27
asked Nov 8 at 9:24
School
599
599
1
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
1
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32
add a comment |
1
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
1
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32
1
1
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
1
1
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32
add a comment |
3 Answers
3
active
oldest
votes
up vote
2
down vote
accepted
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).
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printingunknown
? This should work, so please post your updated code.
– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
|
show 4 more comments
up vote
1
down vote
Instead of text_file.readlines()
use text_file.read()
which will give you contents of files in string format rather than list.
Problem solved, thanks!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
You can do this as follows:
with open("foo","r") as f:
string = f.read()
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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).
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printingunknown
? This should work, so please post your updated code.
– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
|
show 4 more comments
up vote
2
down vote
accepted
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).
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printingunknown
? This should work, so please post your updated code.
– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
|
show 4 more comments
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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).
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).
answered Nov 8 at 9:30
Tomothy32
1,13413
1,13413
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printingunknown
? This should work, so please post your updated code.
– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
|
show 4 more comments
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printingunknown
? This should work, so please post your updated code.
– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
ohh okie I have changed but there are still no output of the text file... @Tomothy32
– School
Nov 8 at 9:37
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Are you getting any error or just no return value? Also, make sure the file name is correct and the file is not empty.
– Tomothy32
Nov 8 at 9:39
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
Just no return value.... no error @Tomothy32
– School
Nov 8 at 9:42
@School Can you try printing
unknown
? This should work, so please post your updated code.– Tomothy32
Nov 8 at 9:44
@School Can you try printing
unknown
? This should work, so please post your updated code.– Tomothy32
Nov 8 at 9:44
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
@Tomoth32 I tried printing out unknown but it didnt give me anything
– School
Nov 8 at 10:09
|
show 4 more comments
up vote
1
down vote
Instead of text_file.readlines()
use text_file.read()
which will give you contents of files in string format rather than list.
Problem solved, thanks!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
Instead of text_file.readlines()
use text_file.read()
which will give you contents of files in string format rather than list.
Problem solved, thanks!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
up vote
1
down vote
Instead of text_file.readlines()
use text_file.read()
which will give you contents of files in string format rather than list.
Instead of text_file.readlines()
use text_file.read()
which will give you contents of files in string format rather than list.
answered Nov 8 at 9:29
bak2trak
46739
46739
Problem solved, thanks!
– School
Nov 8 at 10:58
add a comment |
Problem solved, thanks!
– School
Nov 8 at 10:58
Problem solved, thanks!
– School
Nov 8 at 10:58
Problem solved, thanks!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
You can do this as follows:
with open("foo","r") as f:
string = f.read()
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
You can do this as follows:
with open("foo","r") as f:
string = f.read()
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
add a comment |
up vote
1
down vote
up vote
1
down vote
You can do this as follows:
with open("foo","r") as f:
string = f.read()
You can do this as follows:
with open("foo","r") as f:
string = f.read()
edited Nov 8 at 9:29
Aran-Fey
20.5k53266
20.5k53266
answered Nov 8 at 9:29
power.puffed
115
115
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
add a comment |
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
then how should I write it and return it in my method because I tried to print(string) but there is no output... @power.puffed
– School
Nov 8 at 9:36
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
def check_keyword(): with open("foo.txt", "r") as text_file: unknown = text_file.read() return unknown Printing this as: print(check_keyword()) gives correct output for me, using python 3
– power.puffed
Nov 8 at 9:46
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
it returns me nothing :(( @power.puffed
– School
Nov 8 at 10:00
Problem solved, thanks!!
– School
Nov 8 at 10:58
Problem solved, thanks!!
– School
Nov 8 at 10:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204752%2fhow-do-i-read-a-text-file-as-a-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
If you are returning an open file like that, you're not closing it.
– Tomothy32
Nov 8 at 9:26
1
Sorry, I was replying to @RahulKP
– Tomothy32
Nov 8 at 9:32