spreedsheet中的Python输出而不是.txt文件

时间:2017-08-30 00:50:46

标签: python spreadsheet worksheet

只是玩代码并尝试学习现在我被困住了

这是代码

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.')

我在输出中得到一个文本文件,看起来像这样

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
**************************************************

我想在Microsoft spreedsheet中获取输出。

我试过像

这样的东西
workbook = xlsxwriter.Workbook(screenName + '_followers.xlsx', 'w', 'utf-8')
worksheet = workbook.add_worksheet()

但我无法按行和列排列数据。我希望我的列应该看起来像

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

在Microsoft Excel中

我希望有人会帮助我 提前谢谢你

2 个答案:

答案 0 :(得分:0)

  

问题:我无法按行和列排列数据

执行以下操作:

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

答案 1 :(得分:-1)

我同意上面的评论,使用csv模块可能是要走的路,因为它很容易在Excel中打开,并且似乎没有明显的理由可以获得实际的excel文件?

如果您确实想使用Excel文件格式,请查看问题here

相关问题