Python Forum
Python 2.7.13 Issue Reading .txt files Properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 2.7.13 Issue Reading .txt files Properly
#1
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.
Reply
#2
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/6a5a3...524417761d
Reply
#3
https://docs.python.org/2/library/string...ing-syntax
https://docs.python.org/2/library/stdtyp...l#str.join
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Excel isnt working properly after python function is started IchNar 3 267 May-02-2024, 10:27 AM
Last Post: lillydalson
  How do I properly implement restarting a multithreaded python application? MrFentazis 1 646 Jul-17-2023, 09:10 PM
Last Post: JamesSmith
  [Solved]Help Displaying Emails properly via Python Extra 5 1,204 Sep-28-2022, 09:28 PM
Last Post: deanhystad
  I have an issue with Netmiko Error reading SSH protocol banner omarhegazy 2 3,602 May-16-2022, 06:05 PM
Last Post: omarhegazy
  Reading Multiple text Files in pyhton Fatim 1 1,954 Jun-25-2021, 01:37 PM
Last Post: deanhystad
  Trouble reading files using pyexcel codebeacon 2 2,223 Feb-08-2021, 05:53 AM
Last Post: codebeacon
  Issue with HX711 reading duckredbeard 0 2,213 Aug-28-2020, 10:00 AM
Last Post: duckredbeard
  Reading txt files in ssh server. therenaydin 6 4,212 Aug-06-2020, 05:17 AM
Last Post: ndc85430
  How to stop the reading of files Laplace12 4 2,393 Jul-27-2020, 04:07 PM
Last Post: Gribouillis
  How can I speed up my openpyxl program reading Excel .xlsx files? deac33 0 3,430 May-04-2020, 08:02 PM
Last Post: deac33

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020