0

When I put the code into excel every character is spaced out. This causes Tuesday to look like T,u,e,s,d,a,y in excel. The goal would be to have each cell in excel to have its own word and not the character. There are many for loops and I struggle with finding an answer to this ongoing problem. Any ideas?

import requests
from pprint import pprint
from xml.dom.minidom import parseString
from openpyxl import Workbook

NMNorth2=[("Farmington"),("Gallup"),("Grants"),("Las_Vegas"),("Raton"),("Santa_Fe"), ("Taos"),("Tijeras"),("Tucumcari")]
NMNorth=[("NM", "Farmington"),("NM", "Gallup"),("NM", "Grants"),("NM", "Las_Vegas"),("NM", "Raton"),("NM", "Santa_Fe"), ("NM", "Taos"),("NM", "Tijeras"),("NM", "Tucumcari")]

wb = Workbook()
dest_filename = 'weather.xlsx'
ws1 = wb.active
ws1.title = "Weather"
for state, city in NMNorth:

r = requests.get("http://api.wunderground.com/api/id/forecast/q/"+state+"/"+city+".json")

data = r.json()

forecast = data['forecast']['txt_forecast']['forecastday']
for n in forecast:

    day = n['title']
    forecaststm = (n['fcttext'])
    columnVariable = 2
    for x in day:
        ws1.cell(row = 1, column = columnVariable).value = x
        columnVariable +=1

    for y in forecaststm:
        ws1.cell(row = 2, column = columnVariable).value = y
        columnVariable +=1


rowVariable = 2
ws1.cell(row = 1, column = 1).value = "City"
for state in NMNorth2:
ws1.cell(row = rowVariable, column = 1).value = state
rowVariable +=1

wb.save(filename = dest_filename)
2
  • Hi roganjosh, I did print the items and they came out as words, but are now coming out as characters in excel. Commented Apr 27, 2017 at 17:56
  • Oh. That makes sense. Thank you. You are more than welcome to put your answer in the answer section if you choose. Commented Apr 27, 2017 at 18:10

1 Answer 1

1

The issue here is that python treats strings as iterables. In other words, this bites you if you think you're iterating through a list of strings (or similar) and go one level too deep in nested for loops; the easiest way to identify this is to print what you're working with on each loop.

In your case, the below loop is taking each letter (x) in the day of the week (day), writing it to a column and then incrementing the column you're writing to (columnVariable):

for x in day:
    ws1.cell(row = 1, column = columnVariable).value = x
    columnVariable +=1

Aside, camelCase isn't standard Python, it's more common to use underscores e.g. column_variable. See PEP8

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.