Python Forum
'str' object has no attribute 'to_csv'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'str' object has no attribute 'to_csv'
#1
Lightbulb 
Hello guys,

I'm trying to save some data that I collected from a website textform, on a csv file.
And for that I'm using the following code, but I'm getting the error: 'str' object has no attribute 'to_csv'

import zapimoveis_scraper as zap
import pandas as pd
 
result  = zap.search(localization="sp+sao-paulo", acao="aluguel", tipo="loja-salao", num_pages=5) 
ofertas = len(result)

df = pd.DataFrame([], columns=['Address', 'Price', 'Total Area', 'Bathrooms', 'Badrooms', 'Vacancies', 'Description'])

for i in range(ofertas):
    df = df.append({'Address': result[i].address, 'Price': result[i].price, 'Total Area': result[i].total_area_m2, 'Bathrooms': result[i].bathrooms, 'Badrooms':result[i].bedrooms, 'Vacancies':result[i].vacancies, 'Description':result[i].description}, ignore_index=True)

df.to_csv (r'C:\Users\bruno\Desktop\FII\Zap.csv', index = False, header=True, sep=';', encoding='utf-8-sig')

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select


driver = webdriver.Chrome('C:/bin/chromedriver.exe')
driver.get("https://geocode.localfocus.nl/")
select = Select(driver.find_element_by_id('boundary'))
select.select_by_visible_text("Brazil")

teste = df['Address']

numero = len(teste)
follow_loop = range(0, numero)

for i in follow_loop:
    driver.find_element_by_id("input").send_keys(teste[i] + ", São Paulo"+ "\n")

driver.find_element_by_id("geocode").click()
time.sleep(60)

contents = driver.find_element_by_id('result-geo').get_attribute('value')

contents.to_csv(r'C:\Users\bruno\Desktop\FII\LL.csv')
What's going on? Whey am I getting this error?
How can I save the data from contents on a CSV file?
Reply
#2
Where does the error come from? Line 39 I guess? Why are you trying to call to_csv on a string?
Reply
#3
(Mar-19-2021, 05:57 PM)ndc85430 Wrote: Where does the error come from? Line 39 I guess? Why are you trying to call to_csv on a string?

Yep, the error is on line 39.
How can I convert my string to another format compatible with to_csv?
Reply
#4
What are you actually trying to do with that string? What's in it? If you just want to write it to a CSV file, then the usual methods on file objects should do, but there's always the stuff in the csv module that could help if you need something more advanced. If you need to put the value in a Pandas data frame, then see the docs for how to use that (your two pieces of code look unrelated to me; it looks like you've just taken two scripts and put them into one code block here).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 1,710 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 3,900 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 739 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,367 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,644 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  WebDriver' object has no attribute 'find_element_by_css_selector rickadams 3 5,920 Sep-19-2022, 06:11 PM
Last Post: Larz60+
  df.to_csv('saved_data.csv') Led_Zeppelin 1 1,144 Sep-11-2022, 12:29 AM
Last Post: deanhystad
  'dict_items' object has no attribute 'sort' Calli 6 4,499 Jul-29-2022, 09:19 PM
Last Post: Gribouillis
  AttributeError: 'numpy.ndarray' object has no attribute 'load' hobbyist 8 7,128 Jul-06-2022, 10:55 AM
Last Post: deanhystad
  AttributeError: 'numpy.int32' object has no attribute 'split' rf_kartal 6 4,403 Jun-24-2022, 08:37 AM
Last Post: Anushka00

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020