Python Forum
convert integers to a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert integers to a string
#1
def convert_ceiling(text):
    split = text.split()
    key = 'FEW', 'OVC', 'BKN', 'SCT'
    counter = 0
    for s in split:
        counter += 1
        for k in key:
            if k in s:
                index = split[counter - 1]
                altitude = index.replace(k, '')
                altitude = int(altitude) * 100
                print(altitude)
When I print(altitude), I'm getting:

Quote:1700
900
2400
20000
500

How can I convert those integers into a string with a space so they will be

1700 900 2400 20000 500

I tried
altitude.replace('\n',' ')
but that didn't work. I'm assuming that's because they're integers. I changed them to str, but still not working. I'm not reading a text file just to be clear, I'm getting these data from a website.

And when I call the function, I'm getting 'None'
w = 'KORD 031551Z 28011G18KT 10SM FEW017 OVC009 BKN024 BKN200 17/13 SCT005 A3003 RMK AO2 SLP168 T01720133'
print(convert_ceiling(w))
Reply
#2
print(altitude, end = ' ') will display them with a space in between. However, if you want that in a variable, you'll need to make a list of the string versions of the int, and use the join method of the list.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,223 Jun-09-2023, 06:56 PM
Last Post: kpatil
  convert string to float in list jacklee26 6 1,891 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 1,995 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  how to convert tuple value into string mg24 2 2,313 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  TypeError: string indices must be integers JonWayn 12 3,370 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,368 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert a string to a function mikepy 8 2,490 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,351 Apr-12-2022, 08:31 PM
Last Post: buran
  string indices must be integers when parsing Json ilknurg 3 6,323 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
  Convert string to int Frankduc 8 2,435 Feb-13-2022, 04:50 PM
Last Post: menator01

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020