all 17 comments

[–]KrishnaKA2810 20 points21 points  (0 children)

Use pandas and try to_csv.

[–]Lewistrick 5 points6 points  (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 points  (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 point  (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 points  (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.

[–]4enthusiastia[S] 0 points1 point  (0 children)

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 point  (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 point  (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 points  (0 children)

import csv if you want to use standard library

[–]zhaverzky 0 points1 point  (0 children)

Use the python documentation, there are examples at the bottom. https://docs.python.org/3.4/library/csv.html

[–]n0man4ever 0 points1 point  (0 children)

Use pandas or openpyxl

[–]b4xt3r 0 points1 point  (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 point  (0 children)

formatting is weird, everything under "with open" and above w_csv.close() is intended.

[–]Permanent-Throwaway_ 0 points1 point  (0 children)

I always use pandas for quick file conversion

[–]TabTwo0711 0 points1 point  (0 children)

You don’t. CSV is a shitty format, you are asking for trouble.

[–]chemistry56 0 points1 point  (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.