Python-CSV:如何将数据附加到csv文件中?

时间:2018-06-12 17:27:08

标签: python csv

我已阅读其他question但已尝试但未成功。 data.csv中已经有一行(称为 productId ),我想将数据附加到已创建标题下的新行( productUrl,discount,evaluateScore,volume,packageType,lotNum, validTime,storeName,storeUrl,allImageUrls )。

import csv

def get_details():
    pid = get_id()
    print(pid)
    data = aliexpress.get_product_details(['productId', 'productUrl', 'discount', 'evaluateScore',
                                           'volume', 'packageType', 'lotNum', 'validTime', 'storeName', 'storeUrl',
                                           'allImageUrls'], pid)
    with open('data.csv', 'a', newline='') as csvfile:
        fieldnames = ['productUrl', 'discount', 'evaluateScore',
                                           'volume', 'packageType', 'lotNum', 'validTime', 'storeName', 'storeUrl',
                                           'allImageUrls']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        productUrl = data['productUrl']
        discount = data['discount']
        evaluateScore = data['evaluateScore']
        volume = data['volume']
        packageType = data['packageType']
        lotNum = data['lotNum']
        validTime = data['validTime']
        storeName = data['storeName']
        storeUrl = data['storeUrl']
        allImageUrls = data['allImageUrls']
        allImageUrlstuple = allImageUrls.split(',')
        print(allImageUrls)
        writer.writerow({'productUrl': productUrl, 'discount': discount, 'evaluateScore': evaluateScore,
                                           'volume': volume, 'packageType': packageType, 'lotNum': lotNum, 'validTime': validTime, 'storeName': storeName, 'storeUrl': storeUrl,
                                           'allImageUrls': allImageUrlstuple})

2 个答案:

答案 0 :(得分:0)

如果标头已在CSV中,则无需重新编写标头。删除writer.writeheader()

<强>实施例

import csv

def get_details():
    pid = get_id()
    print(pid)
    data = aliexpress.get_product_details(['productId', 'productUrl', 'discount', 'evaluateScore',
                                           'volume', 'packageType', 'lotNum', 'validTime', 'storeName', 'storeUrl',
                                           'allImageUrls'], pid)
    with open('data.csv', 'a', newline='') as csvfile:
        fieldnames = ['productUrl', 'discount', 'evaluateScore',
                                           'volume', 'packageType', 'lotNum', 'validTime', 'storeName', 'storeUrl',
                                           'allImageUrls']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)

        productUrl = data['productUrl']
        discount = data['discount']
        evaluateScore = data['evaluateScore']
        volume = data['volume']
        packageType = data['packageType']
        lotNum = data['lotNum']
        validTime = data['validTime']
        storeName = data['storeName']
        storeUrl = data['storeUrl']
        allImageUrls = data['allImageUrls']
        allImageUrlstuple = allImageUrls.split(',')
        print(allImageUrls)
        writer.writerow({'productUrl': productUrl, 'discount': discount, 'evaluateScore': evaluateScore,
                                           'volume': volume, 'packageType': packageType, 'lotNum': lotNum, 'validTime': validTime, 'storeName': storeName, 'storeUrl': storeUrl,
                                           'allImageUrls': allImageUrlstuple})

答案 1 :(得分:0)

重写文件可能更容易:

with open('data.csv',newline='') as csvfile:
    r = csv.reader(csvfile)
    data = [line for line in r]

with open('data.csv','w',newline='') as csvfile:
    w = csv.writer(csvfile)
    w.writerow(['col1','col2'])
    w.writerows(data)