<p>你的代码很复杂。我认为这是你所尝试的简化版本。它可能有错误,但如果这不能回答您的问题,请尝试使用它来简化问题中的代码。在</p>
<pre><code>import pandas as pd
import requests
import csv
import json
from datetime import datetime, timedelta
from pandas import DataFrame
from pathlib import Path
DATA_PATH = Path('C:/wab')
input_csv = 'my_data.csv'
output_csv = 'outputempbeets1.csv'
se_ = requests.Session()
df = pd.read_csv(DATA_PATH / input_csv,
usecols=['lat', 'lon', 'field id'],
dtype=str)
dayVal = datetime.strftime(datetime.now() - timedelta(1), '%Y-%m-%d')
dfs = []
def get_high_temp(lat, lon, dayVal):
url = f'https://insight.api.wdtinc.com/daily-high-temperature/{lat}/{lon}'
params = {
'start': f'{dayVal}T00:00:00Z',
'end': f'{dayVal}T01:00:00Z',
'unit': 'fahrenheit',
}
response = se_.get(url,
params=params,
auth=('USERNAME', 'PASSWORD'),
timeout=10)
hi_temp = str(json.loads(response.content)['series'][0]['value'])
return hi_temp
for _, lat, lon, id_ in df.itertuples():
hi_temp = get_high_temp(lat, lon, dayVal)
temp_df = pd.DataFrame({'high temperature': [hi_temp],},
index=[id_])
dfs = dfs.append(temp_df)
pd.concat(dfs).to_csv(DATA_PATH / output_csv, encoding='utf-8')
</code></pre>