Bottom Page

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()))
Quote
#2
Show some sample data from the data.txt file (just a few lines)
Quote
#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

Quote
#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
Quote
#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

Quote
#6
Thanks a lot for your help.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  could not convert string to float: C JamesW 1 102 Jan-22-2020, 08:59 PM
Last Post: Marbelous
  Python convert multi line into single line formatted string karthidec 2 176 Dec-23-2019, 12:46 PM
Last Post: karthidec
  how to convert list to string after append !! evilcode1 3 163 Dec-16-2019, 09:50 AM
Last Post: buran
  Being explicit about the items inside a tuple argument rudihammad 3 180 Dec-04-2019, 08:10 AM
Last Post: perfringo
  Error in the code ->ValueError: could not convert string to float: ' ' eagleboom 1 386 Nov-29-2019, 06:19 AM
Last Post: ThomasL
  convert integers to a string tantony 1 268 Oct-04-2019, 06:40 PM
Last Post: ichabod801
  ValueError: could not convert string to float: . BoaCoder3 3 83,276 Aug-24-2019, 06:26 AM
Last Post: atlass218
  ValueError: could not convert string to float michaelbs007 1 2,833 Jun-08-2019, 08:44 PM
Last Post: micseydel
  convert a string into an int in Python anjita 2 971 Jan-18-2019, 10:37 AM
Last Post: sandeepatel
  Syntactic sugar for explicit lists? Antipaladin 6 1,046 May-11-2018, 06:22 AM
Last Post: volcano63

Forum Jump:


Users browsing this thread: 1 Guest(s)