Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Correctly save list to CSV file

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading