簡體   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()

寫一個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,

他們是對的,但是在.csv文件中寫入數據時,我發現數據的順序與頭部順序不匹配,頭部是:

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

你可以看到在這個方法“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)

可以看到數據項的順序和head里面的一樣,但是我打開csv文件,發現head項的順序是對的,但是數據項的順序是亂的,沒有” id”數據,其他項目順序不正確。

請你幫助我好嗎?

數據結構

問題一:這條線

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

不是在做你想做的事。 該表達式的計算結果始終為True 可能的替代品:

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", }:

和這條線

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

有同樣的問題。 您應該修復這兩行並重試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM