[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

PYTHON HELP: How do I read out only a specific section from a file?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 7
Thread images: 2

File: hqdefault.jpg (29KB, 480x360px) Image search: [Google]
hqdefault.jpg
29KB, 480x360px
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
>>
File: python.png (25KB, 448x667px) Image search: [Google]
python.png
25KB, 448x667px
>>161681
>>161677
I don't wanna code-golf you, but this way is more pythonic, and parses the datasets into objects for later digestion.
Thread posts: 7
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.