How to print set list to new lines in text file python 3
Basically I want to make a program where it reads a text file, does something with the info, and then outputs the different info into a file.
For example;
input.txt contains different lines with text
Then I use this to convert to list:
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
Then outputting the list into a text file not like ['string', 'string'] but on new lines.
Any help with this???
python python-3.x list file file-handling
add a comment |
Basically I want to make a program where it reads a text file, does something with the info, and then outputs the different info into a file.
For example;
input.txt contains different lines with text
Then I use this to convert to list:
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
Then outputting the list into a text file not like ['string', 'string'] but on new lines.
Any help with this???
python python-3.x list file file-handling
What have you tried? This sounds like a homework assignment.
– John Coleman
Nov 24 '18 at 10:53
What have I tried? What do you mean?
– DMM Official
Nov 24 '18 at 10:54
1
what have you done to try solving the problem yourself? @DMMOfficial
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:57
I have searched up online and stack, but I'm just getting started with coding tbh so yeah
– DMM Official
Nov 24 '18 at 11:07
add a comment |
Basically I want to make a program where it reads a text file, does something with the info, and then outputs the different info into a file.
For example;
input.txt contains different lines with text
Then I use this to convert to list:
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
Then outputting the list into a text file not like ['string', 'string'] but on new lines.
Any help with this???
python python-3.x list file file-handling
Basically I want to make a program where it reads a text file, does something with the info, and then outputs the different info into a file.
For example;
input.txt contains different lines with text
Then I use this to convert to list:
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
Then outputting the list into a text file not like ['string', 'string'] but on new lines.
Any help with this???
python python-3.x list file file-handling
python python-3.x list file file-handling
edited Nov 24 '18 at 10:54
DMM Official
asked Nov 24 '18 at 10:50
DMM OfficialDMM Official
73
73
What have you tried? This sounds like a homework assignment.
– John Coleman
Nov 24 '18 at 10:53
What have I tried? What do you mean?
– DMM Official
Nov 24 '18 at 10:54
1
what have you done to try solving the problem yourself? @DMMOfficial
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:57
I have searched up online and stack, but I'm just getting started with coding tbh so yeah
– DMM Official
Nov 24 '18 at 11:07
add a comment |
What have you tried? This sounds like a homework assignment.
– John Coleman
Nov 24 '18 at 10:53
What have I tried? What do you mean?
– DMM Official
Nov 24 '18 at 10:54
1
what have you done to try solving the problem yourself? @DMMOfficial
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:57
I have searched up online and stack, but I'm just getting started with coding tbh so yeah
– DMM Official
Nov 24 '18 at 11:07
What have you tried? This sounds like a homework assignment.
– John Coleman
Nov 24 '18 at 10:53
What have you tried? This sounds like a homework assignment.
– John Coleman
Nov 24 '18 at 10:53
What have I tried? What do you mean?
– DMM Official
Nov 24 '18 at 10:54
What have I tried? What do you mean?
– DMM Official
Nov 24 '18 at 10:54
1
1
what have you done to try solving the problem yourself? @DMMOfficial
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:57
what have you done to try solving the problem yourself? @DMMOfficial
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:57
I have searched up online and stack, but I'm just getting started with coding tbh so yeah
– DMM Official
Nov 24 '18 at 11:07
I have searched up online and stack, but I'm just getting started with coding tbh so yeah
– DMM Official
Nov 24 '18 at 11:07
add a comment |
3 Answers
3
active
oldest
votes
After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:
with open('my_file.txt', 'w') as f:
for item in my_list:
f.write("%sn" % item)
add a comment |
$ cat input.txt
HELLO
WORLD
In python you can use 'n' to append a new line to content.
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
content = 'n'.join(content)
add a comment |
You can create a string like this:
elements_on_new_line = 'n'.join(my_list)
And then you write on the file the variabile 'elements_on_new_line'
Doing this
with open('my file.txt', 'w') as my_file:
my_file.write(elements_on_new_line)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53457377%2fhow-to-print-set-list-to-new-lines-in-text-file-python-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:
with open('my_file.txt', 'w') as f:
for item in my_list:
f.write("%sn" % item)
add a comment |
After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:
with open('my_file.txt', 'w') as f:
for item in my_list:
f.write("%sn" % item)
add a comment |
After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:
with open('my_file.txt', 'w') as f:
for item in my_list:
f.write("%sn" % item)
After reading the file and modifying the content in the list. The list can be written back to a text file with new line for each item in the list as given below:
with open('my_file.txt', 'w') as f:
for item in my_list:
f.write("%sn" % item)
answered Nov 24 '18 at 11:02
Sonal BorkarSonal Borkar
520311
520311
add a comment |
add a comment |
$ cat input.txt
HELLO
WORLD
In python you can use 'n' to append a new line to content.
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
content = 'n'.join(content)
add a comment |
$ cat input.txt
HELLO
WORLD
In python you can use 'n' to append a new line to content.
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
content = 'n'.join(content)
add a comment |
$ cat input.txt
HELLO
WORLD
In python you can use 'n' to append a new line to content.
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
content = 'n'.join(content)
$ cat input.txt
HELLO
WORLD
In python you can use 'n' to append a new line to content.
with open("input.txt") as f:
content = f.readlines()
content = [x.strip() for x in content]
content = 'n'.join(content)
answered Nov 24 '18 at 11:02
Arbazz HussainArbazz Hussain
144114
144114
add a comment |
add a comment |
You can create a string like this:
elements_on_new_line = 'n'.join(my_list)
And then you write on the file the variabile 'elements_on_new_line'
Doing this
with open('my file.txt', 'w') as my_file:
my_file.write(elements_on_new_line)
add a comment |
You can create a string like this:
elements_on_new_line = 'n'.join(my_list)
And then you write on the file the variabile 'elements_on_new_line'
Doing this
with open('my file.txt', 'w') as my_file:
my_file.write(elements_on_new_line)
add a comment |
You can create a string like this:
elements_on_new_line = 'n'.join(my_list)
And then you write on the file the variabile 'elements_on_new_line'
Doing this
with open('my file.txt', 'w') as my_file:
my_file.write(elements_on_new_line)
You can create a string like this:
elements_on_new_line = 'n'.join(my_list)
And then you write on the file the variabile 'elements_on_new_line'
Doing this
with open('my file.txt', 'w') as my_file:
my_file.write(elements_on_new_line)
edited Nov 24 '18 at 11:17
answered Nov 24 '18 at 11:06
91DarioDev91DarioDev
714316
714316
add a comment |
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.
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%2f53457377%2fhow-to-print-set-list-to-new-lines-in-text-file-python-3%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
What have you tried? This sounds like a homework assignment.
– John Coleman
Nov 24 '18 at 10:53
What have I tried? What do you mean?
– DMM Official
Nov 24 '18 at 10:54
1
what have you done to try solving the problem yourself? @DMMOfficial
– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:57
I have searched up online and stack, but I'm just getting started with coding tbh so yeah
– DMM Official
Nov 24 '18 at 11:07