I’ve spent the last two hours trying to figure this out. Please help I’ve tried Google for the past two hours. No luck. What am I doing wrong?
The contents of codes.txt:
TKF6J-KXP6V-F499V-Q9XPC-7J6TZ;J9YCV-D4TH6-WWWWV-F2RM7-F63XZ;GXKCC-QDT34-3JRY4-TWKHX-R763Z;
myscript.py
import re
from sqlalchemy import *
string = open("codes.txt").readlines()
for item in string:
set = [item.split(";")]
print "success"
Why does it print “success” only once? I expect it to print success for the number of items in codes.txt.
readlines()is actually looking for newline characters, and since it doesn’t appear that your string has any, it is only reading one line, and therefore only returning one element (this is what @kayZhu just said, apologies 🙂 ). I think you are looking to split on the semicolon, so you could try this:And then your iteration should work as expected. Also, try to avoid using Python built-in names for your variables (e.g.
string– it will only cause heartache down the road 🙂 ).