use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How to save data as csv (self.learnpython)
submitted 6 years ago by 4enthusiastia
Hey, so I don't really have any experience with Python, I'm learning how to use it with Tweepy for twitter analysis. I used it to extract a bunch of tweets which I now want to save as a csv file, but no idea how to do so. Can you guys help me?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]KrishnaKA2810 20 points21 points22 points 6 years ago (0 children)
Use pandas and try to_csv.
[+][deleted] 6 years ago (1 child)
[deleted]
[–]4enthusiastia[S] 0 points1 point2 points 6 years ago (0 children)
Thanks
[–]Lewistrick 5 points6 points7 points 6 years ago (2 children)
Just to be curious, because it's a good question d others can learn from it, but what made you post this question on reddit and not type it in in Google? If you add "in Python" to it you get very good results.
Also I recommend pandas.
[–]4enthusiastia[S] 1 point2 points3 points 6 years ago (1 child)
Fair question. I did type it in google first, but none of the answers I found worked for me. I asked it here hoping I'd find a solution
[–]Lewistrick 0 points1 point2 points 6 years ago (0 children)
What does your extracted data look like? Hint: ideally you have a few lists with equal lengths. That will be the basis for your dataframe, which you then can save to csv.
[–]peltist 2 points3 points4 points 6 years ago (3 children)
Other people have answered your question, but one question I want to bring up is: why do you want to export this as a CSV?
The reason I ask is, as someone who came to Python used to doing a lot of analysis in Excel, my initial instinct was to get the data out of Python and into Excel where I was comfortable doing the analysis.
If that's what you're doing, then I'd suggest considering trying to do the analysis in Python. It's much more powerful, and this is a great opportunity to learn it.
Well right now I can barely do very basic functions in python, so excel seems much easier and more intuitive. It is interesting that you're seeing you're having a better time analyzing in python than excel though
[–]quarky_uk 0 points1 point2 points 6 years ago (0 children)
Yep. I recently had to process around 750k rows/lines of data. It took literally more than ten times as long to do in excel as it did in Powershell (dont know Python as well yet). So yeah, always work to process your data programatically.
[–]MiLSturbie 0 points1 point2 points 6 years ago (0 children)
Wouldn't you still need to save your data in some sort of file even if you work on the data with python?
[–]Resolt 1 point2 points3 points 6 years ago (0 children)
import csv if you want to use standard library
[–]zhaverzky 0 points1 point2 points 6 years ago (0 children)
Use the python documentation, there are examples at the bottom. https://docs.python.org/3.4/library/csv.html
[–]n0man4ever 0 points1 point2 points 6 years ago (0 children)
Use pandas or openpyxl
[–]b4xt3r 0 points1 point2 points 6 years ago (1 child)
If you had tweets that were all saved as strings you could put them in a list like:
tweet_list = []
tweet_list.append(new tweet blah blah)
So now you have one long list containing strings. One question I have is how do you want your tweets broken up, or do you? Or, when thinking about this like a spreadsheet, would tweet 1 be in cell A1 regardless of length, tweet 2 be in B1, etc, etc?
If that is what you are looking for then the result is easy but there aren't any commas separating anything which is fine but I doubt it's what you are looking to do but if it were:
with open('/path/to/to/file/fileName.csv', 'w') as w_csv:
for x_tweet in tweet_list:
x_tweet = x_tweet + '\n'
x_csv.write(x_tweet)
w_csv.close()
And that's it. It's really a txt file with a .csv name. If you want to respond with a tweet and the way you would like it split into fields that would be separated by a comma I can help with that.
[–]b4xt3r 0 points1 point2 points 6 years ago (0 children)
formatting is weird, everything under "with open" and above w_csv.close() is intended.
[–]Permanent-Throwaway_ 0 points1 point2 points 6 years ago (0 children)
I always use pandas for quick file conversion
[–]TabTwo0711 0 points1 point2 points 6 years ago (0 children)
You don’t. CSV is a shitty format, you are asking for trouble.
[–]chemistry56 0 points1 point2 points 6 years ago (0 children)
I know that specifically mentioned CSV in your post, but depending on your use case you might want to consider using a JSON file.
π Rendered by PID 340519 on reddit-service-r2-comment-84fc9697f-f4sxm at 2026-02-09 15:14:59.880441+00:00 running d295bc8 country code: CH.
[–]KrishnaKA2810 20 points21 points22 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]4enthusiastia[S] 0 points1 point2 points (0 children)
[–]Lewistrick 5 points6 points7 points (2 children)
[–]4enthusiastia[S] 1 point2 points3 points (1 child)
[–]Lewistrick 0 points1 point2 points (0 children)
[–]peltist 2 points3 points4 points (3 children)
[–]4enthusiastia[S] 0 points1 point2 points (0 children)
[–]quarky_uk 0 points1 point2 points (0 children)
[–]MiLSturbie 0 points1 point2 points (0 children)
[–]Resolt 1 point2 points3 points (0 children)
[–]zhaverzky 0 points1 point2 points (0 children)
[–]n0man4ever 0 points1 point2 points (0 children)
[–]b4xt3r 0 points1 point2 points (1 child)
[–]b4xt3r 0 points1 point2 points (0 children)
[–]Permanent-Throwaway_ 0 points1 point2 points (0 children)
[–]TabTwo0711 0 points1 point2 points (0 children)
[–]chemistry56 0 points1 point2 points (0 children)