How to turn a list into a column in a csv?
Currently I have a populated list
list =[a, b, c, d]
I want to convert the content of the list to a csvfile and have all of the elements of the list be one column. This is the current code I have:
with open('twitter3.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(list)
The outputted csv file contains the elements of the list but as the method implies, it has written all the contents within the first row.
I have tried making a for loop to write each element + n
but the writerow
method has an issue with that (puts commas after each letter), and there is no writecolumn
method for a csvwriter
.
python list csv
add a comment |
Currently I have a populated list
list =[a, b, c, d]
I want to convert the content of the list to a csvfile and have all of the elements of the list be one column. This is the current code I have:
with open('twitter3.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(list)
The outputted csv file contains the elements of the list but as the method implies, it has written all the contents within the first row.
I have tried making a for loop to write each element + n
but the writerow
method has an issue with that (puts commas after each letter), and there is no writecolumn
method for a csvwriter
.
python list csv
Are there any other columns?
– ShadowRanger
Nov 24 '18 at 1:48
@ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas
– PaperRockBazooka
Dec 2 '18 at 0:27
add a comment |
Currently I have a populated list
list =[a, b, c, d]
I want to convert the content of the list to a csvfile and have all of the elements of the list be one column. This is the current code I have:
with open('twitter3.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(list)
The outputted csv file contains the elements of the list but as the method implies, it has written all the contents within the first row.
I have tried making a for loop to write each element + n
but the writerow
method has an issue with that (puts commas after each letter), and there is no writecolumn
method for a csvwriter
.
python list csv
Currently I have a populated list
list =[a, b, c, d]
I want to convert the content of the list to a csvfile and have all of the elements of the list be one column. This is the current code I have:
with open('twitter3.csv', 'w+') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerow(list)
The outputted csv file contains the elements of the list but as the method implies, it has written all the contents within the first row.
I have tried making a for loop to write each element + n
but the writerow
method has an issue with that (puts commas after each letter), and there is no writecolumn
method for a csvwriter
.
python list csv
python list csv
edited Nov 24 '18 at 23:45
Cedric Zoppolo
1,34211327
1,34211327
asked Nov 24 '18 at 1:27
PaperRockBazookaPaperRockBazooka
476
476
Are there any other columns?
– ShadowRanger
Nov 24 '18 at 1:48
@ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas
– PaperRockBazooka
Dec 2 '18 at 0:27
add a comment |
Are there any other columns?
– ShadowRanger
Nov 24 '18 at 1:48
@ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas
– PaperRockBazooka
Dec 2 '18 at 0:27
Are there any other columns?
– ShadowRanger
Nov 24 '18 at 1:48
Are there any other columns?
– ShadowRanger
Nov 24 '18 at 1:48
@ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas
– PaperRockBazooka
Dec 2 '18 at 0:27
@ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas
– PaperRockBazooka
Dec 2 '18 at 0:27
add a comment |
4 Answers
4
active
oldest
votes
To perform this task I would use pandas
package as follows:
import pandas as pd
l=["a","b","c","d"]
df=pd.DataFrame(l,index=False,header=False)
df.to_csv("twitter3.csv")
pandas
is also great to read csv and even work with Excel files.
I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!
– PaperRockBazooka
Dec 2 '18 at 0:26
add a comment |
This is about the simplest way I can think of to do it:
import csv
my_list = ['a', 'b', 'c', 'd']
with open('twitter3.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerows(tuple(item) for item in my_list)
Note I changed the name of your list variable to my_list
so it wouldn't conflict with the built-in list
class.
add a comment |
You can do this by joining the elements of your list into a single string with new line characters 'nr'
as the separators, then write the whole string to your file.
For example:
my_list = [a, b, c, d]
with open("twitter3.csv", "w+") as csvfile:
to_write = "nr".join(my_list)
csvfile.write(to_write)
(Also 'n'
works)
add a comment |
I put all the code in one place, so that can copy and test it out.
import csv
# Should not use 'list' as variable
lst =['a', 'b', 'c', 'd']
# newline='' prevent additional new lines in file
with open('twitter3.csv', 'w+', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
for l in lst:
writer.writerow(l)
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%2f53454409%2fhow-to-turn-a-list-into-a-column-in-a-csv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
To perform this task I would use pandas
package as follows:
import pandas as pd
l=["a","b","c","d"]
df=pd.DataFrame(l,index=False,header=False)
df.to_csv("twitter3.csv")
pandas
is also great to read csv and even work with Excel files.
I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!
– PaperRockBazooka
Dec 2 '18 at 0:26
add a comment |
To perform this task I would use pandas
package as follows:
import pandas as pd
l=["a","b","c","d"]
df=pd.DataFrame(l,index=False,header=False)
df.to_csv("twitter3.csv")
pandas
is also great to read csv and even work with Excel files.
I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!
– PaperRockBazooka
Dec 2 '18 at 0:26
add a comment |
To perform this task I would use pandas
package as follows:
import pandas as pd
l=["a","b","c","d"]
df=pd.DataFrame(l,index=False,header=False)
df.to_csv("twitter3.csv")
pandas
is also great to read csv and even work with Excel files.
To perform this task I would use pandas
package as follows:
import pandas as pd
l=["a","b","c","d"]
df=pd.DataFrame(l,index=False,header=False)
df.to_csv("twitter3.csv")
pandas
is also great to read csv and even work with Excel files.
edited Nov 24 '18 at 2:47
answered Nov 24 '18 at 2:39
Cedric ZoppoloCedric Zoppolo
1,34211327
1,34211327
I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!
– PaperRockBazooka
Dec 2 '18 at 0:26
add a comment |
I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!
– PaperRockBazooka
Dec 2 '18 at 0:26
I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!
– PaperRockBazooka
Dec 2 '18 at 0:26
I think this is the way to go. It seems like csv library is just a chore to deal with. None of the suggested solutions worked besides this one!
– PaperRockBazooka
Dec 2 '18 at 0:26
add a comment |
This is about the simplest way I can think of to do it:
import csv
my_list = ['a', 'b', 'c', 'd']
with open('twitter3.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerows(tuple(item) for item in my_list)
Note I changed the name of your list variable to my_list
so it wouldn't conflict with the built-in list
class.
add a comment |
This is about the simplest way I can think of to do it:
import csv
my_list = ['a', 'b', 'c', 'd']
with open('twitter3.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerows(tuple(item) for item in my_list)
Note I changed the name of your list variable to my_list
so it wouldn't conflict with the built-in list
class.
add a comment |
This is about the simplest way I can think of to do it:
import csv
my_list = ['a', 'b', 'c', 'd']
with open('twitter3.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerows(tuple(item) for item in my_list)
Note I changed the name of your list variable to my_list
so it wouldn't conflict with the built-in list
class.
This is about the simplest way I can think of to do it:
import csv
my_list = ['a', 'b', 'c', 'd']
with open('twitter3.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
writer.writerows(tuple(item) for item in my_list)
Note I changed the name of your list variable to my_list
so it wouldn't conflict with the built-in list
class.
answered Nov 24 '18 at 2:04
martineaumartineau
66.2k989178
66.2k989178
add a comment |
add a comment |
You can do this by joining the elements of your list into a single string with new line characters 'nr'
as the separators, then write the whole string to your file.
For example:
my_list = [a, b, c, d]
with open("twitter3.csv", "w+") as csvfile:
to_write = "nr".join(my_list)
csvfile.write(to_write)
(Also 'n'
works)
add a comment |
You can do this by joining the elements of your list into a single string with new line characters 'nr'
as the separators, then write the whole string to your file.
For example:
my_list = [a, b, c, d]
with open("twitter3.csv", "w+") as csvfile:
to_write = "nr".join(my_list)
csvfile.write(to_write)
(Also 'n'
works)
add a comment |
You can do this by joining the elements of your list into a single string with new line characters 'nr'
as the separators, then write the whole string to your file.
For example:
my_list = [a, b, c, d]
with open("twitter3.csv", "w+") as csvfile:
to_write = "nr".join(my_list)
csvfile.write(to_write)
(Also 'n'
works)
You can do this by joining the elements of your list into a single string with new line characters 'nr'
as the separators, then write the whole string to your file.
For example:
my_list = [a, b, c, d]
with open("twitter3.csv", "w+") as csvfile:
to_write = "nr".join(my_list)
csvfile.write(to_write)
(Also 'n'
works)
answered Nov 24 '18 at 1:39
Henry WoodyHenry Woody
3,9692824
3,9692824
add a comment |
add a comment |
I put all the code in one place, so that can copy and test it out.
import csv
# Should not use 'list' as variable
lst =['a', 'b', 'c', 'd']
# newline='' prevent additional new lines in file
with open('twitter3.csv', 'w+', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
for l in lst:
writer.writerow(l)
add a comment |
I put all the code in one place, so that can copy and test it out.
import csv
# Should not use 'list' as variable
lst =['a', 'b', 'c', 'd']
# newline='' prevent additional new lines in file
with open('twitter3.csv', 'w+', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
for l in lst:
writer.writerow(l)
add a comment |
I put all the code in one place, so that can copy and test it out.
import csv
# Should not use 'list' as variable
lst =['a', 'b', 'c', 'd']
# newline='' prevent additional new lines in file
with open('twitter3.csv', 'w+', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
for l in lst:
writer.writerow(l)
I put all the code in one place, so that can copy and test it out.
import csv
# Should not use 'list' as variable
lst =['a', 'b', 'c', 'd']
# newline='' prevent additional new lines in file
with open('twitter3.csv', 'w+', newline='') as csvfile:
writer = csv.writer(csvfile, dialect='excel')
for l in lst:
writer.writerow(l)
answered Nov 24 '18 at 1:41
yoonghmyoonghm
1,076918
1,076918
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%2f53454409%2fhow-to-turn-a-list-into-a-column-in-a-csv%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
Are there any other columns?
– ShadowRanger
Nov 24 '18 at 1:48
@ShadowRanger sorry about the late response. I do not have any other columns at the moment but likely will in the future. I have decided to just rely on pandas
– PaperRockBazooka
Dec 2 '18 at 0:27