-1

Just playing with codes and trying to learn now I am stuck

Here is code

import tweepy
import time
import codecs
import re
import sys
import xlsxwriter

screenName = "abc"

dirf = '/'

consumer_key = 'wabc'
consumer_secret = 'abcs'
access_token = 'abc'
access_secret = 'abcF'

auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)

time1 = time.time()

g = codecs.open( screenName + '_followers.txt', 'w', 'utf-8')

l = []
if (api.verify_credentials):
    print('Login successful')
cycle = 0
users = tweepy.Cursor(api.followers, screen_name=screenName, include_entities=True, include_status=True).items()
while True:
    try:
        user = next(users)
        cycle += 1
        if cycle % 300 == 0: print(cycle)
        fine = True
    except tweepy.TweepError:
        print('sleep 3min')
        time.sleep(3 * 60)
        fine = False
    except StopIteration:
        break
    if fine:
        g.write('screen_name: ' + str(user.screen_name) + '\n')
        g.write('name: ' + str(user.name) + '\n')
        g.write('id_str: ' + str(user.id_str) + '\n')
        g.write('profile_image_url: ' + str(user.profile_image_url_https) + '\n')
        try:
            expanded_url = str(dict(user.entities.get('url', {}).get('urls', [])[0]).get('expanded_url', []))
        except:
            expanded_url = ''
        g.write('url: ' + expanded_url + '\n')
        desc = str(user.description)
        desc = desc.replace('\n', ' ').replace('\r', ' ').replace('\t', '    ')
        g.write('description: ' + desc + '\n')
        g.write('location: ' + str(user.location) + '\n')
        g.write('protected: ' + str(user.protected) + '\n')
        g.write('statuses_count: ' + str(user.statuses_count) + '\n')
        g.write('friends_count: ' + str(user.friends_count) + '\n')
        g.write('followers_count: ' + str(user.followers_count) + '\n')
        g.write('listed_count: ' + str(user.listed_count) + '\n')
        try:
            acd = str(user._json.get('created_at', []))
        except:
            acd = ''
        g.write('acc_creation_date: ' + str(acd) + '\n')
        try:
            last_tweet = str(user.status._json.get('text', []))
            last_tweet_cd = str(user.status._json.get('created_at', []))
            last_tweet = last_tweet.replace('\n', ' ').replace('\r', ' ').replace('\t', '    ')
        except:
            last_tweet = ''
            last_tweet_cd = ''
        g.write('last_tweet: ' + last_tweet_cd + ' ' + last_tweet + '\n')
        g.write('*' * 50 + '\n')

g.close()
time2 = time.time()
difftimeinmin = (time2 - time1) / 60.
print("%.2f minutes" % difftimeinmin)
print('Done.')

I am getting a text file in output which looks like this

screen_name: Happy5725
name: Happy
id_str: 901793150074621953
profile_image_url:e_images/default_profile_normal.png
url: 
description: 
location: 
protected: False
statuses_count: 0
friends_count: 150
followers_count: 3
listed_count: 0
acc_creation_date: Sun Aug 27 13:06:56 +0000 2017
last_tweet:  
**************************************************
screen_name: siachitoba
name: Innocent Siachitoba
id_str: 4529375896
profile_image_url: profile_images/888394481170558977/1KmZsi6d_normal.jpg
url: 
description: 
location: Lusaka, Zambia
protected: False
statuses_count: 17
friends_count: 374
followers_count: 16
listed_count: 0
acc_creation_date: Fri Dec 18 23:00:56 +0000 2015
last_tweet: Tue Aug 29 15:52:08 +0000 2017 My family-Petronella,Mutinta,Mum,Innocent and Chilala. #Godloves /mmjMnkYhfQ
**************************************************
screen_name: eduooko
name: Edwin Ooko
id_str: 626409078
profile_image_url: 
url: 
description: 
location: 
protected: False
statuses_count: 16
friends_count: 75
followers_count: 5
listed_count: 0
acc_creation_date: Wed Jul 04 09:49:29 +0000 2012
last_tweet: Mon Aug 14 13:20:06 +0000 2017 @michaelggitonga hii ndio huitwa kutubeba ujinga
**************************************************

I want to get my output in a Microsoft spreedsheet.

I tried something like

workbook = xlsxwriter.Workbook(screenName + '_followers.xlsx', 'w', 'utf-8')
worksheet = workbook.add_worksheet()

but I wasn't able to arrange my data in rows and columns. I want my columns should be look like

###id    screen name    name     location    description 
..     ..             ..        ..            ..
..     ..             ..        ..            ..

in Microsoft Excel

i hope someone will help me out thank you in advance

Matt Ellen
  • 11,268
  • 4
  • 68
  • 90
Asif Khan
  • 59
  • 5
  • There are libraries to read/write excel directly, but you're usually better off using the `csv` module on the python (which can also do tsv) and then importing that into excel if needed. – o11c Aug 30 '17 at 01:34

2 Answers2

0

Question: I wasn't able to arrange my data in rows and columns

Do the following:

currentRow = 0
while True
    ...

    if fine:
        # Define a Empty List for every Row of Data
        data = []

        # Append each Column Data to the List
        data.append(user.screen_name)
        data.append(user.name)
        data.append(user.id_str)
        data.append(user.profile_image_url_https)
        ... and so on

        # Write all Data to current Row Starting at Column A
        worksheet.write_row(currentRow, 0, data)
        currentRow += 1
stovfl
  • 14,998
  • 7
  • 24
  • 51
-1

I agree with the comment above that using the csv module may be the way to go, as it is easily opened in Excel, and there appears to be no obvious reason for an actual excel file?

If you really want to use an Excel file format, look at the question here:

srattigan
  • 665
  • 5
  • 17