1

I have a CSV file like this:

screenshot of csv file

In Python 3 I am already parsing price data from URL like this:

source1 = "https://www.amazon.com/product1"
source2 = "https://www.ebay.com/product1"
source3 = "https://www.bestbuy.com/product1"

Getting Price data like this:

result1 = trim.sub('', mystring1)
result2 = trim.sub('', mystring2)
result3 = trim.sub('', mystring3)

Now I want to read the above the top CSV file want to feed each row's URL into:

source1 = "read.csv"
source2 = "read.csv"
source3 = "read.csv"

and update the price column using result1, result2, and result3, then go to the next row do the same again.

Can anyone help me how to read and write CSV file like this?

I was able to read the CSV list one by one but when I am trying to write to a new file it is writing like the following picture

enter image description here

Here is the code. It is creating a new row

import csv


with open('compare_sheet', 'w', newline='') as csv_file:
    fieldnames = ['ProductName', 'Amazon', 'Ebay', 'BestBuy']
    writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
    writer.writeheader()

    product = pr_name.text
    result1 = trim.sub('', mystring1)
    print(result1)
    writer.writerow({'ProductName': product, 'Amazon': result1})

    result2 = trim.sub('', mystring2)
    print(result2)
    writer.writerow({'Ebay': result2})

    result3 = trim.sub('', mystring3)
    print(result3)
    writer.writerow({'BestBuy': result3})

How can I write price on same line?

Jason
  • 53
  • 7
  • Start with the "csv" module and show what you tried if you have problems. – Michael Butscher Oct 01 '20 at 01:32
  • 2
    Thanks for the edits, but we still need to see a [mre]. In your final program, you only write a single row. Can you trim this down to a program where you try to write multiple rows, and also show us what error is being displayed (a traceback?) – tripleee Oct 01 '20 at 04:25
  • Hello, Can you check now? – Jason Oct 01 '20 at 06:42
  • Images of text are still [a nuisance](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors), but thanks for the update. – tripleee Oct 01 '20 at 07:03
  • 1
    Tangentially `except Exception` is always a bug. See https://stackoverflow.com/questions/21553327/why-is-except-pass-a-bad-programming-practice – tripleee Oct 01 '20 at 07:21

1 Answers1

0

Like its name says, writerow writes a complete row. You want to assemble all the results into one row, like this;

writer.writerow({'ProductName': product, 'Amazon': result1, 'Ebay': result2, 'BestBuy': result3})

Obviously you can only do this when you have populated all three variables.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I tried it. But then it is only showing one row. When it is going over the loop maybe it is erasing the old row. – Jason Oct 01 '20 at 07:13
  • What is "it"? Your code only examines one product so there will only be one output row. – tripleee Oct 01 '20 at 07:19
  • Oh ... the code you deleted seems to replace the entire CSV file each time you get a new result. You want to append, or better yet only `open` once at the beginning and then keep it open and keep writing until you are done. – tripleee Oct 01 '20 at 07:23