Removing \n from list being split into variables - Python -


so here's code i'm working with:

# initialization twitter oauth open(curdir + '\oauth.txt', 'r') f:     words = f.readlines()     data = [w.replace('\n', '') w in words]  consumer_key = data[0] consumer_secret = data[1] access_key = data[2] access_secret = data[3] auth = tweepy.oauthhandler(consumer_key, consumer_secret) auth.set_access_token(access_key, access_secret) api = tweepy.api(auth)  f.close() 

so basically, i'm trying read each line , deposit them api variables without stuff '\n'. file oauth.txt looks this:

45623sdajhjgsqqwdewf hrjkewrrew892391hnfbndsjhkfb278f93 hb3278dndlwwoerrewewr r3h278cewwooweoifnccvbdgdhsshdgs 

how can use keys alone input vars consumer_key, etc. without other chars '\n'?

thanks lot.

edit: adding reason why yours isn't working.

environment.newline resolves \r\n on windows , \n on unix systems.

given backslash in path, appears you're using windows, meaning \n not correct newline character.

solution 1:

os.linesep

solution 2:

one option that's bit easier create file sort of delimiter. example:

consumer_key=45623sdajhjgsqqwdewf consumer_secret=hrjkewrrew892391hnfbndsjhkfb278f93 access_key=hb3278dndlwwoerrewewr access_secret=r3h278cewwooweoifnccvbdgdhsshdgs 

then can split on delimiter , construct dictionary.

with open(os.path.join(curdir, 'oauth.txt')) f:   lines = f.readlines()   # line regex, test line can split correctly.   line_regex = re.compile('^\w*[=]{1}\w*$')   api = { line.split('=')[0]: line.split('=')[1] line in lines if line_regex.match(line) } 

then have single data structure related data.


Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -