python - Odd character popping up when writing to a file -


new python. i'm testing out files , i'm trying number (taken raw input) written file. want function i've made take number it's input parameter , perform equation it.

for reason though, when writing file strange character pops up. when tried copy-paste up, copied weird block of numbers or empty space. weird character in notepad

here code far:

def function(x):     y = x + 1     return y  input = raw_input('number?')  open('in_test.txt','w+') infile_test:     infile_test.write(input)     lines = infile_test.readline()     lines_int = [int(x) x in lines.split()]     print str(lines_int)  f_test = function(lines_int) print str(f_test) 

i've tried changing file format r+, checking encoding type in notepad (ansi), , looking error comes up.

    lines_int = [int(x) x in lines.split()] valueerror: invalid literal int() base 10: '\x02' 

i'm assuming error caused weird character i'm not sure what's causing weird character.

you need reset file pointer beginning. can check current position of file pointer tell() function.

with open('in_test.txt','w+') infile_test:     infile_test.write(input)     print infile_test.tell()     infile_test.seek(0) # re-position file pointer beginning     lines = infile_test.readline()     lines_int = [int(x) x in lines.split()]     print str(lines_int) 

also, call function() incorrect, you've defined accept int calling list argument.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -