简体   繁体   English

在 csv 文件中的 python 中热转行到列

[英]Hot to transpose rows to columns in python in a csv file

I have the following script that gives me a csv file containing article id, the html code of the article, the url of the article, the date when the article has been created, when the article was last udpated, and the author of the article,我有以下脚本,它为我提供了一个 csv 文件,其中包含文章 ID、文章的 html 代码、url、文章的最后一次创建日期和文章作者的创建时间,

Instead of getting all the outputs in one column I would like each output to be displayed in their own column (transposed).我不想将所有输出放在一列中,而是希望每个 output 显示在它们自己的列中(转置)。

  A.     B.      C.  
1| item | body   | url ...
2| 1234.| <html> | https://..

As you can see in my script, I tried to play around with data transposition but unfortunately the result is the same.正如您在我的脚本中看到的那样,我尝试使用数据转置,但不幸的是结果是相同的。

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])

Please note that I also highly recommend that you use the pandas library .请注意,我还强烈建议您使用pandas 库

However, if you want to transpose a list of lists in plain python, this can also be done easily.但是,如果您想在普通的 python 中转置列表列表,这也可以轻松完成。

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']]

As demonstrated here: Transpose nested list in python如此处所示: 在 python 中转置嵌套列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM