import requests
import pandas as pd
import numpy as np
import os
print("Saving file to:", os.getcwd())
# Define the URL
URL = "https://web.archive.org/web/20230902185 ... nominal%29"
# Fetch the HTML content of the webpage
response = requests.get(URL)
if response.status_code == 200:
print("Successfully fetched the page!")
html_content = response.text
else:
print(f"Failed to fetch the page. Status code: {response.status_code}")
exit()
# Extract tables from the webpage
try:
tables = pd.read_html(html_content)
if not tables:
print("No tables found in the HTML content.")
exit()
except ValueError as e:
print(f"Error reading HTML tables: {e}")
exit()
# Inspect all extracted tables
for i, table in enumerate(tables):
print(f"Table {i}:")
print(table.head())
print("\n")
# Select the required table (adjust index if necessary)
df = tables[3] # Replace 3 with the correct index if needed
print("Selected table:")
print(df.head())
# Dynamically rename and inspect columns
df.columns = range(df.shape[1]) # Replace headers with numerical indices
print("Columns after renaming:", df.columns)
# Handle missing columns dynamically
if 2 in df.columns:
df = df[[0, 2]] # Select columns 0 and 2
else:
print("Column 2 not found. Available columns:", df.columns)
exit()
# Retain rows for the top 10 economies
df = df.iloc[1:11, :]
# Rename columns
df.columns = ['Country', 'GDP (Million USD)']
# Convert GDP from Million USD to Billion USD and round to 2 decimal places
df['GDP (Million USD)'] = df['GDP (Million USD)'].astype(float)
df['GDP (Million USD)'] = np.round(df['GDP (Million USD)'] / 1000, 2)
# Rename the column header to 'GDP (Billion USD)'
df.rename(columns={'GDP (Million USD)': 'GDP (Billion USD)'}, inplace=True)
# Save the DataFrame to a CSV file
df.to_csv(r"C:\Users\Path\Largest_economies.csv", index=False)
print("The top 10 economies by GDP have been saved to 'Largest_economies.csv'.")
< /code>
проверил текущий рабочий каталог с использованием os.getcwd (), явно установив путь к файлу в методе to_csv (), протестированные разрешения на записи, сохранив в более простые местоположения, такие как настольный компьютер, обернутый TO_CSV ( ) в блоке try-except для улавливания ошибок и обеспечения правильного существования кодировки и папок для успешной генерации файлов
Подробнее здесь: https://stackoverflow.com/questions/793 ... nal-window
CSV-файл не создается в ожидаемой папке. Распечатывает в окне терминала ⇐ Python
Программы на Python
1737939165
Anonymous
import requests
import pandas as pd
import numpy as np
import os
print("Saving file to:", os.getcwd())
# Define the URL
URL = "https://web.archive.org/web/20230902185326/https://en.wikipedia.org/wiki/List_of_countries_by_GDP_%28nominal%29"
# Fetch the HTML content of the webpage
response = requests.get(URL)
if response.status_code == 200:
print("Successfully fetched the page!")
html_content = response.text
else:
print(f"Failed to fetch the page. Status code: {response.status_code}")
exit()
# Extract tables from the webpage
try:
tables = pd.read_html(html_content)
if not tables:
print("No tables found in the HTML content.")
exit()
except ValueError as e:
print(f"Error reading HTML tables: {e}")
exit()
# Inspect all extracted tables
for i, table in enumerate(tables):
print(f"Table {i}:")
print(table.head())
print("\n")
# Select the required table (adjust index if necessary)
df = tables[3] # Replace 3 with the correct index if needed
print("Selected table:")
print(df.head())
# Dynamically rename and inspect columns
df.columns = range(df.shape[1]) # Replace headers with numerical indices
print("Columns after renaming:", df.columns)
# Handle missing columns dynamically
if 2 in df.columns:
df = df[[0, 2]] # Select columns 0 and 2
else:
print("Column 2 not found. Available columns:", df.columns)
exit()
# Retain rows for the top 10 economies
df = df.iloc[1:11, :]
# Rename columns
df.columns = ['Country', 'GDP (Million USD)']
# Convert GDP from Million USD to Billion USD and round to 2 decimal places
df['GDP (Million USD)'] = df['GDP (Million USD)'].astype(float)
df['GDP (Million USD)'] = np.round(df['GDP (Million USD)'] / 1000, 2)
# Rename the column header to 'GDP (Billion USD)'
df.rename(columns={'GDP (Million USD)': 'GDP (Billion USD)'}, inplace=True)
# Save the DataFrame to a CSV file
df.to_csv(r"C:\Users\Path\Largest_economies.csv", index=False)
print("The top 10 economies by GDP have been saved to 'Largest_economies.csv'.")
< /code>
проверил текущий рабочий каталог с использованием os.getcwd (), явно установив путь к файлу в методе to_csv (), протестированные разрешения на записи, сохранив в более простые местоположения, такие как настольный компьютер, обернутый TO_CSV ( ) в блоке try-except для улавливания ошибок и обеспечения правильного существования кодировки и папок для успешной генерации файлов
Подробнее здесь: [url]https://stackoverflow.com/questions/79388899/csv-file-not-generating-in-expected-folder-prints-out-in-terminal-window[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия