首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在csv文件中,在python中将行转置为列的热键

在csv文件中,在python中将行转置为列的热键
EN

Stack Overflow用户
提问于 2020-04-06 18:50:13
回答 1查看 104关注 0票数 1

下面的脚本给了我一个csv文件,其中包含文章id、文章的html代码、文章的url、文章的创建日期、文章的最后更新时间以及文章的作者,

我希望每个输出都显示在它们自己的列中(转置),而不是将所有输出放在一列中。

代码语言:javascript
复制
  A.     B.      C.  
1| item | body   | url ...
2| 1234.| <html> | https://..

正如你在我的脚本中看到的,我试着尝试数据转置,但不幸的是结果是一样的。

代码语言:javascript
复制
import requests
import csv
import unicodedata
import getpass

# Set the request parameters
url = ''

# Credentials
user = ''
pwd = ''

# Path of the outputted csv file
csvfile = 'export.csv'

# This loop cycles through all pages of articles, converts the unicode
# to an integer, and writes the integers to an array
output_1 = []
output_1.append("item")
output_2 = []
output_2.append("body")
output_3 = []
output_3.append("html_url")
output_4 = []
output_4.append("created_at")
output_5 = []
output_5.append("updated_at")
output_6 = []
output_6.append("author")

while url:
    response = requests.get(url, auth=(user, pwd))
    data = response.json()
    for article in data['articles']:
        article_id = article['id']
        decode_1 = int(article_id)
        output_1.append(decode_1)
    for article in data['articles']:
        body = article['body']
        decode_2 = unicodedata.normalize('NFKD', body).encode("utf-8")
        output_2.append(decode_2)
    for article in data['articles']:
        html_url = article['html_url']
        decode_3 =unicodedata.normalize('NFKD', html_url)
        output_3.append(decode_3)
    for article in data['articles']:
        created_at = article['created_at']
        decode_4 = unicodedata.normalize('NFKD', created_at)
        output_4.append(decode_4)
    for article in data['articles']:
        updated_at = article['updated_at']
        decode_5 = unicodedata.normalize('NFKD', updated_at)
        output_5.append(decode_5)
    for article in data['articles']:
            author_id = article['author_id']
            decode_6 = int(author_id)
            output_6.append(decode_6)

    print(data['next_page'])
    url = data['next_page']

print("Number of articles:")
print(len(output_1))

# Data Transposition
#nontransposed_data = [("Article ID","Article Body","URL","Created At","Updated At","Author Id"), [output_1], [output_2], [output_3],[output_4],[output_5],[output_6]]
#transposed_data = zip(*nontransposed_data)

# Write to a csv file
with open(csvfile, 'w') as fp:
    writer = csv.writer(fp, dialect='excel')

    writer.writerow([output_1])
    writer.writerow([output_2])
    writer.writerow([output_3])
    writer.writerow([output_4])
    writer.writerow([output_5])
    writer.writerow([output_6])
EN

回答 1

Stack Overflow用户

发布于 2020-04-06 19:48:41

请注意,我也强烈建议您使用pandas library

但是,如果您想在普通python中转置列表列表,这也可以很容易地完成。

代码语言:javascript
复制
nontransposed_data = [('Article ID', 'Article Body', 'URL', 'Created At', 'Updated At', 'Author Id'), ['row 0 col 0', 'row 0 col 1', 'row 0 col 2', 'row 0 col 3', 'row 0 col 4', 'row 0 col 5'], ['row 1 col 0', 'row 1 col 1', 'row 1 col 2', 'row 1 col 3', 'row 1 col 4', 'row 1 col 5'], ['row 2 col 0', 'row 2 col 1', 'row 2 col 2', 'row 2 col 3', 'row 2 col 4', 'row 2 col 5']]


transposed_data = list(map(list, zip(*nontransposed_data)))
transposed_data 

>>> [['Article ID', 'row 0 col 0', 'row 1 col 0', 'row 2 col 0'], ['Article Body', 'row 0 col 1', 'row 1 col 1', 'row 2 col 1'], ['URL', 'row 0 col 2', 'row 1 col 2', 'row 2 col 2'], ['Created At', 'row 0 col 3', 'row 1 col 3', 'row 2 col 3'], ['Updated At', 'row 0 col 4', 'row 1 col 4', 'row 2 col 4'], ['Author Id', 'row 0 col 5', 'row 1 col 5', 'row 2 col 5']]

如下所示:Transpose nested list in python

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61058081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档