0

Text file:

Test 1,15.05.13

Python:

with open("text_file.txt") as inputFile: lines = [line for line in inputFile] var1, var2 = lines[0].strip().split(",") 

This work fine if I have one line. How can I do this if I have many lines?

Text file:

Test 1,15.05.13 Test 4,15.06.13 Test 5,15.07.13 Test 6,15.08.13 

3 Answers 3

1

Use a for-loop. This would return one line at a time from the file(no need to store all lines in memory) and you can apply strip, split on it.

with open("text_file.txt") as inputFile: for line in inputFile: var1, var2 = line.strip().split(",") 
Sign up to request clarification or add additional context in comments.

Comments

0

You can iterate over the lines with a for loop

with open("text_file.txt") as inputFile: lines = [line for line in inputFile] for line in lines: var1, var2 = line.strip().split(",") 

Comments

0

You can put the results into a list as follows:

with open("text_file.txt") as inputFile: vars = [line.strip().split(",") for line in inputfile] 

Result:

>>> vars [['Test 1', '15.05.13'], ['Test 4', '15.06.13'], ['Test 5', '15.07.13'], ['Test 6', '15.08.13']] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.