-1

I have read other question and tried but unsuccessful. There is already 1 row in data.csv (called productId) and I would like to append data into new row under already created headers (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})
DYZ
  • 55,249
  • 10
  • 64
  • 93
Jack AQ
  • 9
  • 1
  • 1
  • 7

2 Answers2

0

You do not need to re-write the header if the header is already in the CSV. Remove writer.writeheader()

Ex:

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})
Rakesh
  • 81,458
  • 17
  • 76
  • 113
  • now it's not writing the headers but new entry not written in correct column instead it appear at the bottom of the page. – Jack AQ Jun 12 '18 at 17:58
  • Is that not what you want. Append new data to csv after existing content ? – Rakesh Jun 12 '18 at 18:06
  • no, append new data under respective headers in existing column. only headers named productIs has it's own contents the others only have headers with empty columns. like this : https://ibb.co/bMaUkJ – Jack AQ Jun 12 '18 at 18:10
  • I need it to start writing from column 6 and skip column 1 to 5 because 1 to 5 already have data in it – Jack AQ Jun 12 '18 at 19:04
0

It may be easier to rewrite the file:

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)
Cibic
  • 316
  • 5
  • 14
  • I need it to start writing from column 6 and skip column 1 to 5 because 1 to 5 already have data in it – Jack AQ Jun 12 '18 at 19:22