简体   繁体   English

python:.csv文件乱写数据

[英]python: write data in .csv file disorder

#write data in .csv file
def data_save_csv(type,data,id,name,header,since = None):
    #get the date when storage data
    date_storage()
    #create the data storage directory
    csv_parent_directory = os.path.join("dataset","csv",type,glovar.date)

    #write data in .csv
    if type == "group_members":
        csv_file_prefix = "gm"
    elif type == "group_feed":
        csv_file_prefix = "gf"
    elif type == "public_figure_posts":
        csv_file_prefix = "pfp"
    elif "user_" in type:
        # create the data storage directory
        csv_parent_directory = os.path.join("dataset", "csv", "user", type, glovar.date)
        if type == "user_friends":
            csv_file_prefix = "uf"
        elif type == "user_likes":
            csv_file_prefix = "ul"
        elif type == "user_feed":
            csv_file_prefix = "uf"
    # create (mkdir) the csv_parent_directory
    directory_create(csv_parent_directory)

    if since:
        csv_file_name = csv_file_prefix + "_" + since.strftime("%Y%m%d-%H%M%S") + "_" + time_storage() + id + "_" +name + ".csv"
    else:
        csv_file_name = csv_file_prefix + "_"  + time_storage() + "_" + id + "_" +name + ".csv"
     csv_file_directory = os.path.join(csv_parent_directory,csv_file_name)

    if type == "user_feed":
        feed = data
        for item in feed:
            # parse the feed data from group_download.py
            print("id=" + item['id'] + ",")
            print("permalink_url=" + item['permalink_url'] + ",")
            print("created_time=" + item['created_time'] + ",")
            print("updated_time=" + item['updated_time'] + ",")
            print("name=" + item['from']['name'] + ",")
            print("from_id=" + item['from']['id'] + ",")
            print("message=" + item['message'] + ",")
            print("link=" + item['link'] + ",")
            print("likes_total_count=" + str(item['likes']['summary']['total_count']) + ",")
            print("comments_total_count=" + str(item['comments']['summary']['total_count']) + ",")

    with open(csv_file_directory,'w',newline='', encoding='utf-8') as csvfile:

        writer = csv.writer(csvfile,delimiter=',',quotechar='"',quoting=csv.QUOTE_MINIMAL)

        #csv header
        writer.writerow(header)

        #if data is group members(group_manage.py)
        if type == "group_members" or "user_friends" or "user_likes":
            row = []
            for i in range(len(data)):
                for k in data[i].keys():
                    if isinstance(data[i][k],bool):
                        data[i][k] = str(data[i][k])
                    row.append(data[i][k])

            writer.writerow(row)
            row = []
        #if data is group feed(group_download.py)
        elif type == "group_feed" or "public_figure_posts" or "user_feed":
            feed = data
            for item in feed:
                #parse the feed data from group_download.py
                row = [item['id'],item['permalink_url'],item['created_time'],item['updated_time'],item['from']['name'],item['from']['id'],item['message'],item['link'],item['likes']['summary']['total_count'],item['comments']['summary']['total_count']]
                writer.writerow(row)

    csvfile.close()

Write a python program to write data in .csv file, when the type is “user_feed”, I print the items of the data:写一个python程序将数据写入.csv文件,当type为“user_feed”时,打印数据项:

id=110286969468305_112459422584393,
permalink_url=https://www.facebook.com/110286969468305/posts/112459422584393,
created_time=2016-12-18T12:44:52+0000,
updated_time=2016-12-18T12:47:10+0000,
name=Dewi Nurfitri Oktaviani,
from_id=10202749157833181,
message=Hi, nice to meet you,
link=,
likes_total_count=0,
comments_total_count=1,

They are right, but when write the data in the .csv file, I found the sequence of the data does not match the head order, the head is :他们是对的,但是在.csv文件中写入数据时,我发现数据的顺序与头部顺序不匹配,头部是:

header = ["POST ID", "Permalink", "Create time", "Updated time", "Author", "Author ID", "Message", "Link", "Likes", "Comments"]

and you can see that in this method "data_save_csv",你可以看到在这个方法“data_save_csv”中,

elif type == "group_feed" or "public_figure_posts" or "user_feed":
    feed = data
    for item in feed:
         #parse the feed data from group_download.py
         row = [item['id'],item['permalink_url'],item['created_time'],item['updated_time'],item['from']['name'],item['from']['id'],item['message'],item['link'],item['likes']['summary']['total_count'],item['comments']['summary']['total_count']]
         writer.writerow(row)

You can see that the sequence of the data item is the same with that in the head, but when I open the csv file, I found the sequence of the head item is right, but the sequence of the data item is disorder, no "id" data, and the other items order is not in the right order.可以看到数据项的顺序和head里面的一样,但是我打开csv文件,发现head项的顺序是对的,但是数据项的顺序是乱的,没有” id”数据,其他项目顺序不正确。

could you please help me?请你帮助我好吗?

数据结构

Problem 1: This line问题一:这条线

if type == "group_members" or "user_friends" or "user_likes":

isn't doing what you want.不是在做你想做的事。 The expression always evaluates to True .该表达式的计算结果始终为True Possible replacements:可能的替代品:

if type == "group_members" or type == "user_friends" or type == "user_likes":

if type in ("group_members", "user_friends", "user_likes", ):

if type in {"group_members", "user_friends", "user_likes", }:

and this line和这条线

elif type == "group_feed" or "public_figure_posts" or "user_feed":

has the same problem.有同样的问题。 You should fix both lines and try again.您应该修复这两行并重试。

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

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