[split] Splitting a string into six pieces - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: [split] Splitting a string into six pieces (/thread-14936.html) |
[split] Splitting a string into six pieces - susmis666 - Dec-25-2018 Hi, I am trying to read data from a text file and split it into six parts. I the code is not able to perform intended function for reasons unidentified.Please help. The text file has below data: Data_Val5 12/25/2018 15:15 406 1 43459635537 Data_Val6 12/25/2018 15:15 379 1 43459635537 Data_Val7 12/25/2018 15:15 352 1 43459635537 Data_Val8 12/25/2018 15:15 325 1 43459635537 Data_Val9 12/25/2018 15:15 298 1 43459635537 Data_Val10 12/25/2018 15:15 270 1 43459635537 Data_Val1 12/25/2018 15:15 514 1 43459635537 The code is as below: #open file dataFile = open('C:\\Users\\USER1\\Desktop\\PyWork\\DataFile_1.txt') for dataStr in dataFile: (d1,d2,d3,d4,d5,d6)=dataStr.split()#split data into six parts. #close file dataFile.close()The output is as below:
RE: [split] Splitting a string into six pieces - ichabod801 - Dec-25-2018 Does your file have any blank lines in it? That could cause the problem. You can be preemptive about it with a conditional: for dataStr in dataFile: if dataStr.strip(): (d1,d2,d3,d4,d5,d6)=dataStr.split()#split data into six parts. |