1

I am trying to export data into csv. When I export it, it is exported as a singular row and each additional data is appended on the same row. The result is a very long row which is difficult to navigate.

The goal is for each value (Time, Wallets) to have their own column and each time the script is ran, the new data is appended to the csv under the respective column. I have tried guides online but wasn't able to find any solutions.

Here is my code if anyone has any suggestions or ideas. Additionally, if anyone has any suggestions for restructuring my code or making it better, I'd love to hear it. As a beginner I would like to learn as much as possible.

#Open ChromeDriver
PATH = ('/Users/chris/Desktop/chromedriver')
driver = webdriver.Chrome(PATH)

#Get Website 

driver.get("https://bitinfocharts.com/top-100-richest-dogecoin-addresses.html")
driver.implicitly_wait(5)
driver.maximize_window()

#Creates the Time
now = datetime.now()
current_time = now.strftime("%m/%d/%Y, %H:%M:%S ")


#Identify the section of page
time.sleep(3)
page = driver.find_element(By.CSS_SELECTOR,'.table.table-condensed.bb')


#Gather the data
time.sleep(3)
num_of_wallets = page.find_element(By.XPATH, "//html/body/div[5]/table[1]/tbody/tr[10]/td[2]").text

#Write to CSV


table_dict = {"Time":current_time,
            "Wallets":num_of_wallets}

headers = ["Time", "Wallets"]

file = open('dogedata.csv', 'a')
file.write(f'{current_time},{num_of_wallets}')
file.close()
3
  • Have you looked into using a Pandas Dataframe to store your data? It's very easy to then export that to a csv as you want (see the docs here) Commented Jan 19, 2022 at 1:42
  • 1
    Add a file.write('\n') before your current file.close(). This should create a new line whenever you write Commented Jan 19, 2022 at 1:51
  • 1
    You should use the csv module in the standard library to create the file. It's built-in, debugged, and easy to use. Commented Jan 19, 2022 at 2:02

1 Answer 1

1

The smallest fix, as Shubham P. pointed out, is to make sure you are writing a "line" by including a line break, like '\n':

file = open('dogedata.csv', 'a')
file.write(f'{current_time},{num_of_wallets}\n')
file.close()

The far better fix, as martineau pointed out, is to use the csv module: it's standard, well-developed and used, and importantly takes care of issues like escaping and quoting characters, and it only takes one more line:

file = open('dogedata.csv', 'a', newline='')
writer = csv.writer(f)
writer.writerow([current_time, num_of_wallets])
file.close()

Note that newline='' was added to the open() function, which tells open() not to handle newlines, and instead defer to the writer which has CSV-specific smarts for dealing w/newlines.

Also, instead of using string formatting, you just wrap your data in a list ([current_time, num_of_wallets]); the writer will convert everything to strings/text.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.