Correctly save list to CSV file

Question:

Using the Selenium library, I get some information (a table from the site) and try to save this table to a CSV file.

elements_xpach_url = browser.find_element(By.XPATH, '/html/body/div[2]/span[2]/table/tbody[2]')
values = re.split('n', elements_xpach_url.text)
df = pd.DataFrame(values)
df.to_csv('csv.csv', index=False, header=False, sep=';')

In the final file, there is no way to set the division as a semicolon ; instead of a space.
enter image description here

    print(type(values))
    print(values)

enter image description here

Could you tell me please, how can this be implemented? Thank you very much!

Asked By: Alex Rebell

||

Answers:

Given your data, you likely want to split your strings on spaces:

values = [re.split() for x in re.split('n', elements_xpach_url.text)]
df = pd.DataFrame(values)

Note that you do not need re.split to split on newlines, str.split is enough:

values = [re.split() for x in elements_xpach_url.text.split('n')]
df = pd.DataFrame(values)
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.