Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
filing in Python 3.7
#1
I have an eternal file in this format: "4b32j","aq8","71yt346" only much longer. I want to load each item one at a time for processing. I can't find how to do this, so I tried to load the whole file and split it up. But it loads as a complete string anyway. I used the code below:

path = '/Users/davidellis/Documents/Python 3 Files/docCodes_File.txt'
test = open(path, 'r')
play = [test.read()]
test.close()
print (play)
This just gives me [' "4b32j","aq8","71yt346" ']. If I load it as a string, it still comes in as strings within a string.
Can you suggest how I could open the file and load just one item at a time sequentially, please? I've searched Python help, but could only find material on loading specific lengths or lines, not items.
Reply
#2
is the whole file one line (it looks like)? can you post part of the file content in output tags
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
#3
For simplicity and to keep the file size down, I just created this text file and saved it as Test_File:

"This", "file", "was", "created", "to", "practice", "file", "operations", "in", "Python"

Here's the result of running the code as already posted:

Output:
>>> ===== RESTART: /Users/davidellis/Documents/Python 3 Files/scratchPad.py ===== ['"This", "file", "was", "created", "to", "practice", "file", "operations", "in", "Python"'] >>>
Reply


Forum Jump:

User Panel Messages

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