convert non-string with explicit base - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: convert non-string with explicit base (/thread-13821.html) |
convert non-string with explicit base - jacklee26 - Nov-02-2018 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())) RE: convert non-string with explicit base - Larz60+ - Nov-02-2018 Show some sample data from the data.txt file (just a few lines) RE: convert non-string with explicit base - ichabod801 - Nov-02-2018 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. RE: convert non-string with explicit base - jacklee26 - Nov-02-2018 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 RE: convert non-string with explicit base - ichabod801 - Nov-02-2018 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. RE: convert non-string with explicit base - jacklee26 - Nov-06-2018 Thanks a lot for your help. |