简体   繁体   中英

Python replace cells of CSV file

I have a html scraper which pulls titles of items and prices from a website. Once in a while i want to run this scraper to update my prices, doing so i also wish to keep the old ones.

my csv where i save the titles and prices in for the first time looks like this:

Title1, price1, 'END'
Title2, price2, 'END'
Title3, price3, 'END'

I compare the new price against the old with the following method:

ind = row.index('END')
lastprijs = row[ind-1]
print lastprijs
if lastprijs != prijstitel:
    row.pop(ind)
    row.append(prijstitel)
    row.append("END")

if a value has been found (and set) i update the csv with

with open('out.csv', 'a') as out:
    tester = csv.writer(out)
    tester.writerow(row)

if no value has been found i update the csv with the same row:

else: 
    with open('out.csv', 'a') as out:
        tester = csv.writer(out)
        row.append("addedddd")   #add a new line.
        tester.writerow(row)

However the output of my csv is as following after running it:

Item1, price1, 'END'
Item2, price2, 'END'
Item3, price3, 'END'
Item1, price1, 'END'
item1, price1, 'END'
Item2, price2, 'END'
Item3, price3, 'END'
Item1, price1, 'END'
item1, price1, 'END'
Item2, price2, 'END'
Item2, price2, 'END'
Item3, price3, 'END'
Item1, price1, 'END'

And so on... How can i fix this?

** THE FULL CODE **

def updateprices(prijstitel, titelprijs):
  with open('pricewatch.csv', 'r') as csvfileadjust:
    filereader = csv.reader(csvfileadjust)
    print titelprijs
    if titelprijs == "Gembird externe Hardeschijf behuizing met USB 2.0 aansluiting":
        prijstitel = 'EDITED PRIJS!'
    for row in filereader:
        header = row
        print header
        print " ---- "
        if titelprijs in row:
            ind = row.index('END')
            lastprijs = row[ind-1]
            print lastprijs
            if lastprijs != prijstitel:
                row.pop(ind)
                row.append(prijstitel)
                row.append("END")
            with open('out.csv', 'a') as out:
                tester = csv.writer(out)
                tester.writerow(row)
        else: 
            with open('out.csv', 'a') as out:
                tester = csv.writer(out)
                row.append("addedddd")   #add a new line.
                tester.writerow(row)

You were right with your problem,

This fixes your problem

x=0
while x < 20:  Or length of csv file.
   updateprices("10,00","TITELS", x)
   x+=1

def updateprices(prijstitel, titelprijs, var):
  with open(csvfile, 'r') as csvfileadjust:  #open the file
    filereader = csv.reader(csvfileadjust)
    row = list(islice(filereader,var+1))[-1]  #get all lines till var+1
    if titelprijs in row:  #if the title is in the row
        ind = row.index('END')  #search for END in that List
        lastprijs = row[ind-1] 
        print lastprijs
        if lastprijs != prijstitel:  #if lastprijs is not equal to prijstitel ( 9,99 != 10,00)
            row.pop(ind)  #drop the last item in list ("END")
            row.append(prijstitel)  #add the new prijstitel (10,00)
            row.append("END")  
        with open('out.csv', 'a') as out:
            tester = csv.writer(out)  
            tester.writerow(row)   #Write to csv file
    else:   #if the title is not in the row
        with open('out.csv', 'a') as out:
            tester = csv.writer(out)
            tester.writerow(row)  #write (or copy) the line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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