Issue uploading data using FTP and Python
이전 댓글 표시
I have weather data that goes to an FTP site (only way to store it) I use Python to transfer from FTP to Thingspeak. If I upload all the data at once using import all my data comes into thingspeak. When I use Python I only get one maybe two records. Do I need to upload every minute?
sample code (manual push)
# Connect to FTP and download the file into memory
try:
ftp = ftplib.FTP(ftp_server)
ftp.login(username, password)
# Create an in-memory binary stream for the file
memory_file = io.BytesIO()
ftp.retrbinary('RETR ' + ftp_file_path, memory_file.write)
ftp.quit()
# Go to the start of the memory file
memory_file.seek(0)
# Read CSV file from memory
data = pd.read_csv(memory_file)
# Process and send each row of the CSV to ThingSpeak
for index, row in data.iterrows():
payload = {'api_key': api_key}
for i, value in enumerate(row):
payload[f'field{i+1}'] = value
response = requests.post(update_url, params=payload)
if response.status_code == 200:
print(f'Data sent successfully: {row}')
else:
print(f'Error sending data: {row}, Status Code: {response.status_code}')
except ftplib.all_errors as e:
print(f"FTP error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
채택된 답변
추가 답변 (0개)
커뮤니티
더 많은 답변 보기: ThingSpeak 커뮤니티
카테고리
도움말 센터 및 File Exchange에서 Write Data to Channel에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!