How to save my python output to csv automatically The Next CEO of Stack OverflowHow do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Using python, how to use collect tweets (using tweepy) between two dates?
I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin
Can we say or write : "No, it'sn't"?
Disadvantage of gaining multiple levels at once in a short milestone-XP game
What does convergence in distribution "in the Gromov–Hausdorff" sense mean?
Why do remote companies require working in the US?
What does "Its cash flow is deeply negative" mean?
Is HostGator storing my password in plaintext?
At which OSI layer a user-generated data resides?
Unreliable Magic - Is it worth it?
If the heap is initialized for security, then why is the stack uninitialized?
Interfacing a button to MCU (and PC) with 50m long cable
How to transpose the 1st and -1th levels of arbitrarily nested array?
How do I go from 300 unfinished/half written blog posts, to published posts?
Giving the same color to different shapefiles in QGIS
How should I support this large drywall patch?
What happened in Rome, when the western empire "fell"?
Workaholic Formal/Informal
Different harmonic changes implied by a simple descending scale
Novel about a guy who is possessed by the divine essence and the world ends?
Rotate a column
Help understanding this unsettling image of Titan, Epimetheus, and Saturn's rings?
Indicator light circuit
Why am I allowed to create multiple unique pointers from a single object?
What flight has the highest ratio of time difference to flight time?
How to save my python output to csv automatically
The Next CEO of Stack OverflowHow do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?Finding the index of an item given a list containing it in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?How to get the current time in PythonHow can I make a time delay in Python?Does Python have a string 'contains' substring method?Using python, how to use collect tweets (using tweepy) between two dates?
Below is the code I am trying to save the results I get from my print status to csv or json
# Creating the authentication object
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# Setting your access token and secret
auth.set_access_token(access_token, access_token_secret)
# Creating the API object while passing in auth information
api = tweepy.API(auth)
# The Twitter user who we want to get tweets from
name = "mytwitterid"
# Number of tweets to pull
tweetCount = 10
for status in tweepy.Cursor(api.home_timeline).items(10):
# Process a single status
print(status.text)
#result =
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for row in status.text():
csvwriter.writerow(row)
This throws error for row in status.text():
TypeError: 'str' object is not callable
python
|
show 1 more comment
Below is the code I am trying to save the results I get from my print status to csv or json
# Creating the authentication object
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# Setting your access token and secret
auth.set_access_token(access_token, access_token_secret)
# Creating the API object while passing in auth information
api = tweepy.API(auth)
# The Twitter user who we want to get tweets from
name = "mytwitterid"
# Number of tweets to pull
tweetCount = 10
for status in tweepy.Cursor(api.home_timeline).items(10):
# Process a single status
print(status.text)
#result =
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for row in status.text():
csvwriter.writerow(row)
This throws error for row in status.text():
TypeError: 'str' object is not callable
python
...as the error message says:status.text(without the brackets) the way you use that a few lines above.
– hiro protagonist
Mar 21 at 17:01
Meansstatus.textisstr. Just use it as it is
– han solo
Mar 21 at 17:04
Instead ofstatus.text()in yourforloop, just usestatus.text
– Green Cloak Guy
Mar 21 at 17:08
thank you all it works after I remove () but I get blank csv. I don't not see any records
– Martin
Mar 21 at 17:41
@Martin: As it stands, this code should throw an error:statusis local tofor status in ...therefore check your indentation and show the output ofprint(status.text). Second, the parameterrowmust be asequence, looping astrdoesn't result in asequence.
– stovfl
Mar 21 at 18:02
|
show 1 more comment
Below is the code I am trying to save the results I get from my print status to csv or json
# Creating the authentication object
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# Setting your access token and secret
auth.set_access_token(access_token, access_token_secret)
# Creating the API object while passing in auth information
api = tweepy.API(auth)
# The Twitter user who we want to get tweets from
name = "mytwitterid"
# Number of tweets to pull
tweetCount = 10
for status in tweepy.Cursor(api.home_timeline).items(10):
# Process a single status
print(status.text)
#result =
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for row in status.text():
csvwriter.writerow(row)
This throws error for row in status.text():
TypeError: 'str' object is not callable
python
Below is the code I am trying to save the results I get from my print status to csv or json
# Creating the authentication object
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# Setting your access token and secret
auth.set_access_token(access_token, access_token_secret)
# Creating the API object while passing in auth information
api = tweepy.API(auth)
# The Twitter user who we want to get tweets from
name = "mytwitterid"
# Number of tweets to pull
tweetCount = 10
for status in tweepy.Cursor(api.home_timeline).items(10):
# Process a single status
print(status.text)
#result =
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for row in status.text():
csvwriter.writerow(row)
This throws error for row in status.text():
TypeError: 'str' object is not callable
python
python
asked Mar 21 at 17:00
MartinMartin
83
83
...as the error message says:status.text(without the brackets) the way you use that a few lines above.
– hiro protagonist
Mar 21 at 17:01
Meansstatus.textisstr. Just use it as it is
– han solo
Mar 21 at 17:04
Instead ofstatus.text()in yourforloop, just usestatus.text
– Green Cloak Guy
Mar 21 at 17:08
thank you all it works after I remove () but I get blank csv. I don't not see any records
– Martin
Mar 21 at 17:41
@Martin: As it stands, this code should throw an error:statusis local tofor status in ...therefore check your indentation and show the output ofprint(status.text). Second, the parameterrowmust be asequence, looping astrdoesn't result in asequence.
– stovfl
Mar 21 at 18:02
|
show 1 more comment
...as the error message says:status.text(without the brackets) the way you use that a few lines above.
– hiro protagonist
Mar 21 at 17:01
Meansstatus.textisstr. Just use it as it is
– han solo
Mar 21 at 17:04
Instead ofstatus.text()in yourforloop, just usestatus.text
– Green Cloak Guy
Mar 21 at 17:08
thank you all it works after I remove () but I get blank csv. I don't not see any records
– Martin
Mar 21 at 17:41
@Martin: As it stands, this code should throw an error:statusis local tofor status in ...therefore check your indentation and show the output ofprint(status.text). Second, the parameterrowmust be asequence, looping astrdoesn't result in asequence.
– stovfl
Mar 21 at 18:02
...as the error message says:
status.text (without the brackets) the way you use that a few lines above.– hiro protagonist
Mar 21 at 17:01
...as the error message says:
status.text (without the brackets) the way you use that a few lines above.– hiro protagonist
Mar 21 at 17:01
Means
status.text is str. Just use it as it is– han solo
Mar 21 at 17:04
Means
status.text is str. Just use it as it is– han solo
Mar 21 at 17:04
Instead of
status.text() in your for loop, just use status.text– Green Cloak Guy
Mar 21 at 17:08
Instead of
status.text() in your for loop, just use status.text– Green Cloak Guy
Mar 21 at 17:08
thank you all it works after I remove () but I get blank csv. I don't not see any records
– Martin
Mar 21 at 17:41
thank you all it works after I remove () but I get blank csv. I don't not see any records
– Martin
Mar 21 at 17:41
@Martin: As it stands, this code should throw an error:
status is local to for status in ... therefore check your indentation and show the output of print(status.text). Second, the parameter row must be a sequence, looping a str doesn't result in a sequence.– stovfl
Mar 21 at 18:02
@Martin: As it stands, this code should throw an error:
status is local to for status in ... therefore check your indentation and show the output of print(status.text). Second, the parameter row must be a sequence, looping a str doesn't result in a sequence.– stovfl
Mar 21 at 18:02
|
show 1 more comment
1 Answer
1
active
oldest
votes
You are just printing and discarding the things you loop over; after the for loop; status.text will only contain the last status. Try this instead:
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for status in tweepy.Cursor(api.home_timeline).items(10):
print(status.text)
csvwriter.writerow([status.text])
If you want to collect the results into a list variable which you then dump out as a single JSON file, use append in the loop.
results = [] # start with an empty list
for status in tweepy.Cursor(api.home_timeline).items(10):
#print(status.text)
results.append(status.text)
# Now dump results as JSON
with open('results.json', 'w') as outputfile:
json.dump(results, outputfile)
works as a charm, how do I remove the spaces and include ',' for each tweet?
– Martin
Mar 21 at 19:37
Not sure what the format looks like exactly. If that's your attempt at producing JSON, read the tweets into a list withappendand then justjson.dumpit after the loop.
– tripleee
Mar 21 at 20:25
I tried below for json does not work though json.dump(status._json,file,sort_keys = True,indent = 4) return alltweets
– Martin
Mar 22 at 20:00
That does not seem connected to the code in your question. Post a new question with enough details if you can't figure it out.
– tripleee
Mar 22 at 21:31
@Martin See updated answer with a simple JSON example now,
– tripleee
Mar 23 at 7:53
|
show 1 more 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%2f55285622%2fhow-to-save-my-python-output-to-csv-automatically%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are just printing and discarding the things you loop over; after the for loop; status.text will only contain the last status. Try this instead:
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for status in tweepy.Cursor(api.home_timeline).items(10):
print(status.text)
csvwriter.writerow([status.text])
If you want to collect the results into a list variable which you then dump out as a single JSON file, use append in the loop.
results = [] # start with an empty list
for status in tweepy.Cursor(api.home_timeline).items(10):
#print(status.text)
results.append(status.text)
# Now dump results as JSON
with open('results.json', 'w') as outputfile:
json.dump(results, outputfile)
works as a charm, how do I remove the spaces and include ',' for each tweet?
– Martin
Mar 21 at 19:37
Not sure what the format looks like exactly. If that's your attempt at producing JSON, read the tweets into a list withappendand then justjson.dumpit after the loop.
– tripleee
Mar 21 at 20:25
I tried below for json does not work though json.dump(status._json,file,sort_keys = True,indent = 4) return alltweets
– Martin
Mar 22 at 20:00
That does not seem connected to the code in your question. Post a new question with enough details if you can't figure it out.
– tripleee
Mar 22 at 21:31
@Martin See updated answer with a simple JSON example now,
– tripleee
Mar 23 at 7:53
|
show 1 more comment
You are just printing and discarding the things you loop over; after the for loop; status.text will only contain the last status. Try this instead:
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for status in tweepy.Cursor(api.home_timeline).items(10):
print(status.text)
csvwriter.writerow([status.text])
If you want to collect the results into a list variable which you then dump out as a single JSON file, use append in the loop.
results = [] # start with an empty list
for status in tweepy.Cursor(api.home_timeline).items(10):
#print(status.text)
results.append(status.text)
# Now dump results as JSON
with open('results.json', 'w') as outputfile:
json.dump(results, outputfile)
works as a charm, how do I remove the spaces and include ',' for each tweet?
– Martin
Mar 21 at 19:37
Not sure what the format looks like exactly. If that's your attempt at producing JSON, read the tweets into a list withappendand then justjson.dumpit after the loop.
– tripleee
Mar 21 at 20:25
I tried below for json does not work though json.dump(status._json,file,sort_keys = True,indent = 4) return alltweets
– Martin
Mar 22 at 20:00
That does not seem connected to the code in your question. Post a new question with enough details if you can't figure it out.
– tripleee
Mar 22 at 21:31
@Martin See updated answer with a simple JSON example now,
– tripleee
Mar 23 at 7:53
|
show 1 more comment
You are just printing and discarding the things you loop over; after the for loop; status.text will only contain the last status. Try this instead:
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for status in tweepy.Cursor(api.home_timeline).items(10):
print(status.text)
csvwriter.writerow([status.text])
If you want to collect the results into a list variable which you then dump out as a single JSON file, use append in the loop.
results = [] # start with an empty list
for status in tweepy.Cursor(api.home_timeline).items(10):
#print(status.text)
results.append(status.text)
# Now dump results as JSON
with open('results.json', 'w') as outputfile:
json.dump(results, outputfile)
You are just printing and discarding the things you loop over; after the for loop; status.text will only contain the last status. Try this instead:
with open('output.csv', 'w') as csvfile:
csvwriter = csv.writer(csvfile)
for status in tweepy.Cursor(api.home_timeline).items(10):
print(status.text)
csvwriter.writerow([status.text])
If you want to collect the results into a list variable which you then dump out as a single JSON file, use append in the loop.
results = [] # start with an empty list
for status in tweepy.Cursor(api.home_timeline).items(10):
#print(status.text)
results.append(status.text)
# Now dump results as JSON
with open('results.json', 'w') as outputfile:
json.dump(results, outputfile)
edited Mar 23 at 7:52
answered Mar 21 at 18:05
tripleeetripleee
95.1k13133189
95.1k13133189
works as a charm, how do I remove the spaces and include ',' for each tweet?
– Martin
Mar 21 at 19:37
Not sure what the format looks like exactly. If that's your attempt at producing JSON, read the tweets into a list withappendand then justjson.dumpit after the loop.
– tripleee
Mar 21 at 20:25
I tried below for json does not work though json.dump(status._json,file,sort_keys = True,indent = 4) return alltweets
– Martin
Mar 22 at 20:00
That does not seem connected to the code in your question. Post a new question with enough details if you can't figure it out.
– tripleee
Mar 22 at 21:31
@Martin See updated answer with a simple JSON example now,
– tripleee
Mar 23 at 7:53
|
show 1 more comment
works as a charm, how do I remove the spaces and include ',' for each tweet?
– Martin
Mar 21 at 19:37
Not sure what the format looks like exactly. If that's your attempt at producing JSON, read the tweets into a list withappendand then justjson.dumpit after the loop.
– tripleee
Mar 21 at 20:25
I tried below for json does not work though json.dump(status._json,file,sort_keys = True,indent = 4) return alltweets
– Martin
Mar 22 at 20:00
That does not seem connected to the code in your question. Post a new question with enough details if you can't figure it out.
– tripleee
Mar 22 at 21:31
@Martin See updated answer with a simple JSON example now,
– tripleee
Mar 23 at 7:53
works as a charm, how do I remove the spaces and include ',' for each tweet?
– Martin
Mar 21 at 19:37
works as a charm, how do I remove the spaces and include ',' for each tweet?
– Martin
Mar 21 at 19:37
Not sure what the format looks like exactly. If that's your attempt at producing JSON, read the tweets into a list with
append and then just json.dump it after the loop.– tripleee
Mar 21 at 20:25
Not sure what the format looks like exactly. If that's your attempt at producing JSON, read the tweets into a list with
append and then just json.dump it after the loop.– tripleee
Mar 21 at 20:25
I tried below for json does not work though json.dump(status._json,file,sort_keys = True,indent = 4) return alltweets
– Martin
Mar 22 at 20:00
I tried below for json does not work though json.dump(status._json,file,sort_keys = True,indent = 4) return alltweets
– Martin
Mar 22 at 20:00
That does not seem connected to the code in your question. Post a new question with enough details if you can't figure it out.
– tripleee
Mar 22 at 21:31
That does not seem connected to the code in your question. Post a new question with enough details if you can't figure it out.
– tripleee
Mar 22 at 21:31
@Martin See updated answer with a simple JSON example now,
– tripleee
Mar 23 at 7:53
@Martin See updated answer with a simple JSON example now,
– tripleee
Mar 23 at 7:53
|
show 1 more 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%2f55285622%2fhow-to-save-my-python-output-to-csv-automatically%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
...as the error message says:
status.text(without the brackets) the way you use that a few lines above.– hiro protagonist
Mar 21 at 17:01
Means
status.textisstr. Just use it as it is– han solo
Mar 21 at 17:04
Instead of
status.text()in yourforloop, just usestatus.text– Green Cloak Guy
Mar 21 at 17:08
thank you all it works after I remove () but I get blank csv. I don't not see any records
– Martin
Mar 21 at 17:41
@Martin: As it stands, this code should throw an error:
statusis local tofor status in ...therefore check your indentation and show the output ofprint(status.text). Second, the parameterrowmust be asequence, looping astrdoesn't result in asequence.– stovfl
Mar 21 at 18:02