Python Forum
Python 2.7.13 Issue Reading .txt files Properly - 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: Python 2.7.13 Issue Reading .txt files Properly (/thread-16996.html)



Python 2.7.13 Issue Reading .txt files Properly - username1145 - Mar-23-2019

Hi all,

I'm using a Raspberry Pi, and I can only run programs using python 2.7.13, and it's been a hassle. For one of my programs, I need to read information from a file and each row contains 10 characters. The program I listed below works exactly how I want with python 3.5.3, but not with python 2.7.13. The output should look like this:

00 - 0 - - - -
01 - - - 1 - -
02 - - - - 1 -
00 0 - - - - -
01 - - - 1 - -

When I run it with 2.7.13, I get this abomination:

('00', '-', '0', '-', '-', '-', '-')
('', '', '', '', '', '', '')
('', '', '', '', '', '', '')
('-', '', '', '', '', '', '')
('--', '', '', '', '', '', '')

Here's the code:
instruct = open('song1.txt', 'r')
n = 0
while n != 5:
	loadFile = instruct.read(10).splitlines()
	a = loadFile[0][:2]
	b = loadFile[0][3:4]
	c = loadFile[0][4:5]
	d = loadFile[0][5:6]
	e = loadFile[0][6:7]
    f = loadFile[0][7:8]
    g = loadFile[0][8:9]
	n += 1
	print (a,b,c,d,e,f,g)
instruct.close()
If someone could help me in any possible way it would be greatly appreciated.


RE: Python 2.7.13 Issue Reading .txt files Properly - Larz60+ - Mar-23-2019

Quote:I'm using a Raspberry Pi, and I can only run programs using python 2.7.13, and it's been a hassle.
see this: https://gist.github.com/SeppPenner/6a5a30ebc8f79936fa136c524417761d


RE: Python 2.7.13 Issue Reading .txt files Properly - buran - Mar-24-2019

https://docs.python.org/2/library/string.html#format-string-syntax
https://docs.python.org/2/library/stdtypes.html#str.join


RE: Python 2.7.13 Issue Reading .txt files Properly - username1145 - Mar-24-2019

Larz60+

Sorry I should have elaborated on that. I have Python 3.7 installed. The issue is I am using a Pi to run an individually addressable LED strip, but the libraries only have support for Python 2.7.13. So even if I were to try
$ sudo python3 strandtest.py
it wouldn't work.

I didn't mention this because I felt it to be very out of place in this forum.