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
#1
Hi i'm facing on an error:
File "MAC_add1+2.py", line 15, in <module>
line = int(line,16)+2
TypeError: int() can't convert non-string with explicit base

Do anyone who why?
if i just put "line = int(line,16)+1" it will be fine, but if i add another line "line = int(line,16)+2", it will pop out an error.

Do anyone know how to solve this issue.


with open('data.txt') as f,open('data_out.txt', 'w') as f_out:
    for line in f:
        line = line.strip()
	f_out.write('{}'.format(line))
	line = int(line,16)+1
	f_out.write(('\t{}'.format(hex(line).rstrip("L").lstrip("0x") or "0").expandtabs(5).upper()))
	line = int(line,16)+2
	f_out.write(('\t{}'.format(hex(line).rstrip("L").lstrip("0x") or "0").expandtabs(5).upper()))
Reply
#2
Show some sample data from the data.txt file (just a few lines)
Reply
#3
On line 3, the line variable is set to a string. On line 5, you convert the string to an integer with an explicit base, which is fine. But the line variable is now an int. So when you try to convert it again on line 7, it throws an error because you can only convert a string with an explicit base, and line is now an int.

You don't need to convert it the second time. It's already an int. If you need one to it, just add one to line.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
Hi Craig,
Thanks for explain the code.
It work now, i just add integer to it.
line = int+2

HI I have another question would like to ask
How do i add a for loop and print each line from 1 to 10
with open('data.txt') as f,open('data_out.txt', 'w') as f_out:
f_out.write(('===\t=========== \t =========== \t =========== \n').expandtabs(15))
    for line in f:
		#the line variable is set to a string
        line = line.strip()
	f_out.write('{}'.format(line))
right now it will print like this :

1234567890 1234567891 1234567892
i wants to print like this

1 1234567890 1234567891 1234567892
2 1234567890 1234567891 1234567892
.............
10 1234567890 1234567891 1234567892


i just wants to add a range from 1 to 10, but i have no idea where to put the loop.

Thanks
Reply
#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
#6
Thanks a lot for your help.
Reply


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