在Python中将2个新列写入CSV文件
我试图将列添加到“CSV”文件中,我可以从上一个CSV文件复制,但当我尝试写入下两列时,它仅使用最后一行填充其余行,例如在Python中将2个新列写入CSV文件,python,csv,Python,Csv,我试图将列添加到“CSV”文件中,我可以从上一个CSV文件复制,但当我尝试写入下两列时,它仅使用最后一行填充其余行,例如speed和wind\u dirs。这两个图形是在for循环中创建的,我是否应该将它们存储在列表中,然后从列表中写入?任何想法都很好。谢谢 with open('latlon.csv', 'r') as csvfile: with open('wind2.csv', 'w') as csvoutput: towns_csv = csv.reader(cs
speed
和wind\u dirs
。这两个图形是在for
循环中创建的,我是否应该将它们存储在列表中,然后从列表中写入?任何想法都很好。谢谢
with open('latlon.csv', 'r') as csvfile:
with open('wind2.csv', 'w') as csvoutput:
towns_csv = csv.reader(csvfile, dialect='excel') # csv reader
writer = csv.writer(csvoutput, lineterminator='\n') # csv writer
for rows in towns_csv:
x = float(rows[2]) #gets x axis
y = float(rows[1]) # gets y axis
url = ("http://api.met.no/weatherapi/locationforecast/1.9/?") #start of url string
lat = "lat="+format(y) #creates the latititue part of the url string
lon = "lon="+format(x) # creates the longitude part of the url string
text = url + format(lat) + ";" + format(lon) #combines the strings together to create a new url
response = requests.get(text) # get the url
xml_text=response.text # turns the requested url into a text file
winds= bs4.BeautifulSoup(xml_text, "xml") #uses BeautifulSoup to make an xml file
wind_all = winds.find_all("windSpeed") # finds the "windSpeed" element
speed = wind_all[0].get("mps") # finds the first "mps" attribute
wind_dir = winds.find_all("windDirection")# finds the "windDirection" element
wind_dirs = wind_dir[0].get("deg") #finds the "deg" attribute
new = []
row = next(towns_csv)
row.append(speed)
row.append(wind_dirs)
new.append(row)
for item in towns_csv:
item.append(speed)
item.append(wind_dirs)
new.append(item)
writer.writerow(item)
考虑以下调整,包括对字符串格式的轻微修改和最后几行代码的修改。对于循环,不需要行或新列表以及最后一个
。只需将API数据附加到latlong.csv
文件的行中,并将其写入新文件wind2.csv
:
import csv
import requests
import bs4
with open('latlon.csv', 'r') as csvfile, open('wind2.csv', 'w') as csvoutput:
towns_csv = csv.reader(csvfile, dialect='excel') # csv reader
writer = csv.writer(csvoutput, lineterminator='\n') # csv writer
for rows in towns_csv:
x = float(rows[2]) # gets x axis
y = float(rows[1]) # gets y axis
url = "http://api.met.no/weatherapi/locationforecast/1.9/?{0};{1}" # start of url string
lat = "lat={}".format(y) # creates the latititue part of the url string
lon = "lon={}".format(x) # creates the longitude part of the url string
text = url.format(lat, lon) # combines the strings together to create a new url
response = requests.get(text).text # get the url into text format
winds= bs4.BeautifulSoup(response, "xml") # uses BeautifulSoup to make an xml file
wind_all = winds.find_all("windSpeed") # finds the "windSpeed" element
speed = wind_all[0].get("mps") # finds the first "mps" attribute
wind_dir = winds.find_all("windDirection") # finds the "windDirection" element
wind_dirs = wind_dir[0].get("deg") # finds the "deg" attribute
rows.append(speed) # append speed value
rows.append(wind_dirs) # append wind value
writer.writerow(rows) # write new row
输入(latlon.csv)
输出(wind2.csv)
考虑以下调整,包括对字符串格式的轻微修改和最后几行代码的修改。对于
循环,不需要行或新列表以及最后一个。只需将API数据附加到latlong.csv
文件的行中,并将其写入新文件wind2.csv
:
import csv
import requests
import bs4
with open('latlon.csv', 'r') as csvfile, open('wind2.csv', 'w') as csvoutput:
towns_csv = csv.reader(csvfile, dialect='excel') # csv reader
writer = csv.writer(csvoutput, lineterminator='\n') # csv writer
for rows in towns_csv:
x = float(rows[2]) # gets x axis
y = float(rows[1]) # gets y axis
url = "http://api.met.no/weatherapi/locationforecast/1.9/?{0};{1}" # start of url string
lat = "lat={}".format(y) # creates the latititue part of the url string
lon = "lon={}".format(x) # creates the longitude part of the url string
text = url.format(lat, lon) # combines the strings together to create a new url
response = requests.get(text).text # get the url into text format
winds= bs4.BeautifulSoup(response, "xml") # uses BeautifulSoup to make an xml file
wind_all = winds.find_all("windSpeed") # finds the "windSpeed" element
speed = wind_all[0].get("mps") # finds the first "mps" attribute
wind_dir = winds.find_all("windDirection") # finds the "windDirection" element
wind_dirs = wind_dir[0].get("deg") # finds the "deg" attribute
rows.append(speed) # append speed value
rows.append(wind_dirs) # append wind value
writer.writerow(rows) # write new row
输入(latlon.csv)
输出(wind2.csv)
这在我看来并不正确:writer.writerow(行+[speed]+[wind\u dirs])
。您是否打算写行
(单数)?我正在努力想象你在那行代码中创建的结构,在zip
之后,不幸的是我自己无法运行它来查看结果。你的编辑现在可能破坏了你的代码。您已经为行嵌套了。编辑:现在你又把它改回来了。我发表这些评论并不是建议你继续编辑——你发布的代码是否准确地表达了你正在使用的内容?这是很重要的一部分,如果你不断地改变它,没有人能帮助你调试。writer.writerow
的使用是正确的。它需要一个列表,列表中的每个元素都是输出的一列。使用zip
然后在调用中连接列表感觉很笨拙,我认为您得到的列表格式不正确,但我无法测试。基本上,您可能只是想从以前的csv(将是一个列表)中获取一行,然后将额外的列扩展到该列表中,然后将其写回。这听起来像是我试图做的,但只是做得很差,我将尝试创建一个列表,并将这两个新内容附加到列表的末尾。谢谢你的帮助,我真的很感激。所以我基本上已经弄明白了,我编辑了上面的代码,我现在遇到的问题是,它似乎没有在速度和风的方向上迭代,只使用循环中的最后一个数字。我觉得这不太合适:writer.writerow(rows+[speed]+[wind_dirs])
。您是否打算编写行
(单数)?我很难想象你在那行代码中创建的结构,也就是在zip
之后,不幸的是我自己无法运行它来查看结果。你的编辑现在可能已经破坏了你的代码。你已经为行嵌套了。编辑:现在你已经将其更改回来了。我发表这些评论不是为了建议你eep编辑-您发布的代码是否准确地表示了您正在使用的内容?这是重要的一部分,如果您不断更改,没有人可以帮助您进行调试。writer.writerow
的使用是正确的。它需要一个列表,列表中的每个元素都是输出的一列。使用zip
,然后连接g该调用中的列表感觉很笨拙,我认为您得到的列表格式不正确,但我无法测试。本质上,您可能只是想从上一个csv中获取一行(这将是一个列表)只需将额外的列扩展到列表中,然后写回。这听起来像是我正在尝试做的,但只是做得很差,我将尝试创建一个列表,并将这两个新内容附加到列表的末尾。感谢您的帮助,我真的很感激。所以我基本上明白了,我编辑了上面的代码,问题是我现在遇到的问题是,它似乎没有在速度
和风速
上迭代,只是使用循环中的最后一个数字。效果很好,我不知道为什么我试图把事情复杂化。谢谢。这很好,我不知道为什么我要把事情复杂化。谢谢。
1 54 -122 1.3 16.2
2 53 -112 4 330.7
3 52 -102 4.9 314.1