Last active
September 21, 2023 22:35
-
-
Save 3Kmfi6HP/7a94c435321800e07dce33a949a4989a to your computer and use it in GitHub Desktop.
down csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from telethon.tl.types import DocumentAttributeFilename | |
import os | |
async def download_csv_files(client, entity): | |
messages = await client.get_messages(entity, limit=None) | |
for message in messages: | |
if hasattr(message, 'media') and message.media: # Check if the message has media | |
media = message.media | |
if hasattr(media, 'document') and media.document: # Check if the media is a document | |
document = media.document | |
if hasattr(document, 'attributes'): # Check if the document has attributes | |
for attribute in document.attributes: | |
if isinstance(attribute, DocumentAttributeFilename): # Check if the attribute is a filename | |
filename = attribute.file_name | |
if filename.endswith('.csv'): # Check if the filename ends with '.csv' | |
print(f"Downloading {filename}...") | |
local_path = os.path.join('downloads', filename) # Construct the local file path | |
await client.download_media(message, local_path) # Download the media to the local path | |
print(f"Downloaded {filename}") | |
# Get the entity (chat, channel, or user) | |
entity = await client.get_entity(-1001821044133) | |
await download_csv_files(client, entity) | |
import os | |
import csv | |
# 定义文件夹路径 | |
folder_path = "downloads" | |
# 获取目录中的csv文件列表 | |
csv_files = [file for file in os.listdir(folder_path) if file.endswith(".csv")] | |
# 合并csv文件 | |
output_file = "merged.csv" | |
with open(output_file, mode='w', newline='') as merged_file: | |
writer = csv.writer(merged_file) | |
for file in csv_files: | |
with open(os.path.join(folder_path, file), mode='r') as csv_file: | |
reader = csv.reader(csv_file) | |
next(reader) # 跳过第一行 | |
for row in reader: | |
writer.writerow(row) | |
import os | |
import csv | |
# Define folder path | |
folder_path = "downloads" | |
# Get a list of CSV files in the directory | |
csv_files = [file for file in os.listdir(folder_path) if file.endswith(".csv")] | |
# Merge CSV files | |
output_file = "merged.csv" | |
with open(output_file, mode='w', newline='', encoding='utf-8') as merged_file: | |
writer = csv.writer(merged_file) | |
for file in csv_files: | |
with open(os.path.join(folder_path, file), mode='r', encoding='utf-8') as csv_file: | |
reader = csv.reader(csv_file) | |
next(reader) # Skip the first row | |
for row in reader: | |
writer.writerow(row) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment