Python Forum
convert non-string with explicit base
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert non-string with explicit base
#5
Well, you've already got the loop:

    for line in f:
        line = line.strip()
To print, just add the print to it:

    for line in f:
        line = line.strip()
        f_out.write('{}'.format(line))
If you want the line numbers, and you want to stop at 10, you should add enumerate to the loop. Enumerate takes something you would loop over, and loops of the indexes of that thing and that thing (as a tuple).

    for line_index, line in enumerate(f):
        line = line.strip()
        if line_index < 10:
            f_out.write('{} {}'.format(line_index + 1, line))
I put the +1 in the last line because enumerate starts at 0, like Python indexes do.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Messages In This Thread
RE: convert non-string with explicit base - by ichabod801 - Nov-02-2018, 12:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  convert string to float in list jacklee26 6 1,964 Feb-13-2023, 01:14 AM
Last Post: jacklee26
Thumbs Up Convert an Interger into any base !? [Solved] SpongeB0B 8 1,454 Jan-16-2023, 10:24 AM
Last Post: SpongeB0B
  how to convert tuple value into string mg24 2 2,403 Oct-06-2022, 08:13 AM
Last Post: DeaD_EyE
  Convert string to float problem vasik006 8 3,458 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  is there any tool to convert negative base to int? Skaperen 7 2,446 May-27-2022, 07:30 AM
Last Post: Gribouillis
  Convert a string to a function mikepy 8 2,590 May-13-2022, 07:28 PM
Last Post: mikepy
Question How to convert string to variable? chatguy 5 2,501 Apr-12-2022, 08:31 PM
Last Post: buran
  Convert string to int Frankduc 8 2,528 Feb-13-2022, 04:50 PM
Last Post: menator01
  Convert string to path using Python 2.7 tester_V 10 6,546 Nov-20-2021, 02:20 PM
Last Post: snippsat
  Convert each element of a list to a string for processing tester_V 6 5,405 Jun-16-2021, 02:11 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

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