I've been stuck on this problem for days and literally can't seem to figure it out. In the code below, how do I extract DATASET 1 and 2 separately and put them into the lists x1, y1 and x2. y2?
Here is my code so far:
myoutput = """\
DATASET: IMPORTANT TITLE 1
DATE: 12/30/2015
X Y
1 100
2 120
3 125
4 135
5 180
6 300
7 500
8 600
9 1000
10 10000
DATASET: IMPORTANT TITLE 2
DATE: 01/03/2016
X Y
1 100
2 130
3 145
4 535
5 680
6 700
7 800
8 900
9 3000
10 50000
"""
lines = myoutput.splitlines()
print(lines)
for line in lines[8:]:
words = line.split()
x_axis = words[0]
y_axis = words[1]
>>161667
And if I print out to screen this, I get the error: >IndexError: list index out of range
I've been following an example here under the section: III. Intermediate Python,
Output Parsing http://nbviewer.jupyter.org/gist/rpmuller/5920182#Recursion-and-Factorials
>>161667
#!/usr/bin/bash
import os
import time
file_name = "test.txt"
lists = [[], []]
listnr = -1
try:
file = open(file_name, "r")
except IOError:
print("Something went wrong writing to ", file_name)
sys.exit()
i = 1
current = file.readline()
while current != "":
if "DATASET" in current:
listnr = listnr + 1
print(listnr, ": ", current, end="")
if listnr != -1:
if current[0].isdigit():
lists[listnr].append(int(current.split()[1]))
i += 1
current = file.readline()
print(lists, end="")
file.close()
Written just for you anon
>>161677
Whoops I borked the tabs.
Also something to note I only tried it in py3.
#!/usr/bin/bash
import os
import time
file_name = "test.txt"
lists = [[], []]
listnr = -1
try:
file = open(file_name, "r")
except IOError:
print("Something went wrong writing to ", file_name)
sys.exit()
i = 1
current = file.readline()
while current != "":
if "DATASET" in current:
listnr = listnr + 1
print(listnr, ": ", current, end="")
if listnr != -1:
if current[0].isdigit():
lists[listnr].append(int(current.split()[1]))
i += 1
current = file.readline()
print(lists, end="")
file.close()
>>161681
ok i didn't understand any of that.
don't know how nested the if loops are because its not tabbed here
also what is listnr?
>>161692
I could explain it all but it would be useless if you don't know the basics. I suggest looking for some python3 tutorials on youtube.
Here is a nice place to start:
https://www.youtube.com/watch?v=cpPG0bKHYKc
>>161681
>>161677
I don't wanna code-golf you, but this way is more pythonic, and parses the datasets into objects for later digestion.