Why won't kaggle accept splicing code the VSC does? - python

I wrote a for loop in VSC to splice some data and it worked perfectly. Now that I'm trying to import it to Kaggle, it will not work and keeps giving returning this error: "IndexError: string index out of range". The data I'm using has many random letters before the useful info. I'm trying to cut that information out in order to analyze it.
This is the code that worked for VSC. Is there a different method of splicing strings that I should try? Also the picture has the code I used, for some reason it wouldn't let me insert it here.
These are the strings that I am indexing:['SIT' 'FEED:DS:FRUIT' 'FEED:SY:FLOWERS' 'FEED:PR:YOUNG LEAVES'
'FEED:PR:LEAF BUDS' 'MOVE BETWEEN' 'GROOM' 'FEED:CA:BARK OR PITH'
'FEED:?:EPIPHYTES' 'FEED:LI:LEAF SURFACE' 'FEED:?:?' 'FEED:FB:FRUIT'
'FEED:PR:EPIPHYTES' 'FEED:FE:FRUIT' 'FEED:CA:?' 'FEED:ER:UNSP. LEAVES'
'FEED:?:DEAD LEAVES' 'MOVE WITHIN' 'OTHER SOCIAL' 'FEED:OL:FRUIT'
'FEED:UV:FRUIT' 'FEED:14:FRUIT' 'FEED:FN:FRUIT' 'FEED:MO:LEAF BUDS'
'FEED:ER:FLOWERS' 'FEED:ER:?' 'FEED:ER:MATURE LEAVE' 'LIE' 'FEED:?:FRUIT'
'FEED:?:DEAD WOOD' 'FEED:PA:EPIPHYTES' 'FEED:ER:YOUNG LEAVES'
'FEED:FD:FRUIT' 'FEED:CD:EPIPHYTES' 'FEED:LI:DEAD LEAVES'
'FEED:FD:EPIPHYTES' 'FEED:PA:DEAD BARK' 'FEED:CD:FRUIT'
'FEED:FT:DEAD WOOD' 'FEED:FB:LEAF BUDS' 'FEED:DS:?' 'FEED:18:FLOWERS'
'FEED:PR:FRUIT' 'FEED:19:FRUIT' 'FEED:NB:BARE SURFACE'
'FEED:ML:YOUNG LEAVES' 'FEED:PL:DEAD LEAVES' 'FEED:OL:EPIPHYTES' 'STAND'
'FEED:MK:EPIPHYTES' 'FEED:DS:LEAF SURFACE' 'FEED:FC:YOUNG LEAVES'
'FEED:FB:EPIPHYTES' 'FEED:CO:EPIPHYTES' 'FEED:12:LEAF BUDS'
'FEED:?:DEAD BARK' 'FEED:PA:DEAD WOOD' 'FEED:FC:FRUIT'
'FEED:SP:DEAD BARK' 'FEED:NT:UNSP. LEAVES' 'FEED:ML:FRUIT'
'FEED:28:FRUIT' 'FEED:36:FRUIT' 'FEED:MK:FRUIT' 'FEED:LI:STICKS'.......] There are many others.
I got it to look like in VSC: ['SIT' 'FEED: FRUIT' 'FEED: FLOWERS' 'FEED: YOUNG LEAVES'
'FEED: LEAF BUDS' 'MOVE BETWEEN' 'GROOM' 'FEED: BARK OR PITH'
'FEED: EPIPHYTES' 'FEED: LEAF SURFACE' 'FEED UNKNOWN'
'FEED: UNSP. LEAVES' 'FEED: DEAD LEAVES' 'MOVE WITHIN' 'OTHER SOCIAL'
'FEED: MATURE LEAVE' 'LIE' 'FEED: DEAD WOOD' 'FEED: DEAD BARK'
'FEED: BARE SURFACE' 'STAND' 'FEED: STICKS' 'DRINK' 'SELF-GROOM' 'PLAY'
'FEED: ' 'CLING']

Related

Problem with a new revised code for pyflowchart

I am having an error labeled "
**Exception has occurred: TypeError
Node.__init__() takes 1 positional argument but 2 were given
File "C:\Users\::::::\::::::\::::::\folder\*****.py", line 36, in <module>
start_node = Node('Start')**
"
This is for a school project about hydroponic farming as you might be able to tell from the code :)
# Import flowchart libraries
from pyflowchart.flowchart import Flowchart, Node
# Define blocks for flowchart
def identify_goals():
print("Identify goals for project, such as producing a certain amount of vegetables for the school cafeteria within a certain time frame.")
def choose_location():
print("Choose a suitable indoor location on the school grounds, such as a greenhouse or a room with good lighting.")
def gather_materials():
print("Gather necessary materials and equipment, such as buckets, fertilizers, watering systems, and light sources.")
def prepare_containers():
print("Prepare buckets and other containers to hold the plant material.")
def choose_vegetables():
print("Choose the vegetables to be grown, and obtain seeds or plant starters.")
def plant_seeds():
print("Plant the seeds or starters in the buckets or containers.")
def set_up_system():
print("Set up light sources and watering systems.")
def monitor_and_adjust():
print("Monitor and adjust the system as needed to ensure optimal growth of the plants.")
def harvest_vegetables():
print("Harvest the vegetables when they are ripe and ready to be eaten.")
def restart_process():
print("Restart the process so that the school can have a continuous supply of fresh vegetables.")
# Create start node
start_node = Node('Start')
# Create flowchart
pyflowchart = Flowchart(head_node=start_node)
# Add blocks to flowchart
pyflowchart.add_block('Start', {'Next': 'identify_goals'})
pyflowchart.add_block(identify_goals, {'Next': 'choose_location'})
pyflowchart.add_block(choose_location, {'Next': 'gather_materials'})
pyflowchart.add_block(gather_materials, {'Next': 'prepare_containers'})
pyflowchart.add_block(prepare_containers, {'Next': 'choose_vegetables'})
pyflowchart.add_block(choose_vegetables, {'Next': 'plant_seeds'})
pyflowchart.add_block(plant_seeds, {'Next': 'set_up_system'})
pyflowchart.add_block(set_up_system, {'Next': 'monitor_and_adjust'})
pyflowchart.add_block(monitor_and_adjust, {'Next': 'harvest_vegetables'})
pyflowchart.add_block(harvest_vegetables, {'Next': 'restart_process'})
pyflowchart.add_block(restart_process, {'Next': 'End'})
pyflowchart.add_block('End')
pyflowchart.draw('flowchart.png')
The code is giving an error which I am unsure how to fix, any help is much appreciated :)
if anyone has more experience with making flowcharts in python, or just in general python it would help a lot

For loop writing rows into variables

I am trying to write a loop in python to extract information from a sentence per row. THe input sentences look like this:
[t] troubleshooting ad-2500 and ad-2600 no picture scrolling b/w .
##repost from january 13 , 2004 with a better fit title .
i/p button[+2]##im a more happier person after discovering the i/p button !
dvd player[+1][p]##it practically plays almost everything you give it .
player[+2],sound[-1]##i 've had the player for about 2 years now and it still performs nicely with the exception of an occasional wwhhhrrr sound from the motor .
I would like to extract the sentences only and use the information before the ## as tags and write this all to a variable that then contains all the information. The expected output:
Variable: title
troubleshooting ad-2500 and ad-2600 no picture scrolling b/w .
troubleshooting ad-2500 and ad-2600 no picture scrolling b/w .
troubleshooting ad-2500 and ad-2600 no picture scrolling b/w .
troubleshooting ad-2500 and ad-2600 no picture scrolling b/w .
troubleshooting ad-2500 and ad-2600 no picture scrolling b/w .
So the variable should be maintained until a new [t] is in the row.
Variable: sentence_only
repost from january 13 , 2004 with a better fit title .
im a more happier person after discovering the i/p button !
it practically plays almost everything you give it .
i 've had the player for about 2 years now and it still performs nicely with the exception of an occasional wwhhhrrr sound from the motor .
Variable: tag
i/p button[+2]
dvd player[+1][p]
player[+2],sound[-1]
The current output only maintains the last row and not the full list in the variable.
Here is my attempt in solving this:
import nltk
from nltk.corpus import PlaintextCorpusReader
corpus_root = "Data/Customer_review_data"
filelists = PlaintextCorpusReader(corpus_root, '.*')
filelists.fileids()
rawlist = filelists.raw('Apex AD2600 Progressive-scan DVD player.txt')
sentence = rawlist.split("\n")[:]
a_line = ""
sentence_only = ""
content = ""
title = ""
tag = ""
for b_line in sentence:
if title != '' or content != '' or sentence_only != '':
content = title, tag, sentence_only
if re.match(r"^\*", b_line):
continue
if re.match(r"^\[t\][ ]", b_line):
title = b_line[4:]
continue
if re.match(r"^\[t\]", b_line):
title = b_line[3:]
continue
if re.match(r"^##", b_line):
sentence_only = b_line[2:]
continue
if re.match(r".*##", b_line):
i = len(b_line.split('##')[0])+2
sentence_only = b_line[i:]
tag = b_line[:i-2]
continue
if re.match(r".*#", b_line):
sentence_only = b_line[2:]
continue
print(test)
Actually, I reread your question, and it seems each file only contains one item. If that is the case, you can do this much easier.
with open("somefile.txt") as infile:
data = infile.read().splitlines() # this seems to work OS agnostic
item = {
"title": data[0][4:],
"contents": [{"tag": line.split("##")[0], "sentence": line.split("##")[1]} for line in data[1:]]
}
This will result in a dict item that is the same as the ones in the old answer below...
OLD ANSWER
I would use a list of dict items to contain the data, but you can easily adjust what variables you put the resulting data in.
from pprint import pprint
with open("somefile.txt") as infile:
data = infile.read().splitlines() # this seems to work OS agnostic
result = []
current_item = None
for line in data:
if line.startswith('[t]'):
# add everything stored sofar to result
# check is needed for the first loop
if current_item:
result.append(current_item)
current_item = {
"title": line[4:], # strip the [t] part
"contents": [] # reset the contents list
}
else:
current_item["contents"].append({
"tag": line.split("##")[0], # the first element of the split
"sentence": line.split("##")[1] # the second element of the split
})
# finally, add last item
result.append(current_item)
# usage:
for item in result:
print(f"\nTITLE: {item['title']}")
print("Variable: sentence_only")
for content in item["contents"]:
print(content["sentence"])
for item in result:
print(f"\nTITLE: {item['title']}")
print("Variable: tag")
for content in item["contents"]:
print(content["tag"])
# pprint:
pprint(result)
Output below.
Note that I just duplicated the example input and added the very imaginative NR2 to the lines to differentiate between the two "items" in the source file...
TITLE: troubleshooting ad-2500 and ad-2600 no picture scrolling b/w .
Variable: sentence_only
repost from january 13 , 2004 with a better fit title .
im a more happier person after discovering the i/p button !
it practically plays almost everything you give it .
i 've had the player for about 2 years now and it still performs nicely with the exception of an occasional wwhhhrrr sound from the motor .
TITLE: troubleshooting ad-2500 and ad-2600 no picture scrolling b/w . NR2
Variable: sentence_only
repost from january 13 , 2004 with a better fit title . NR2
im a more happier person after discovering the i/p button ! NR2
it practically plays almost everything you give it . NR2
i 've had the player for about 2 years now and it still performs nicely with the exception of an occasional wwhhhrrr sound from the motor . NR2
TITLE: troubleshooting ad-2500 and ad-2600 no picture scrolling b/w .
Variable: tag
i/p button[+2]
dvd player[+1][p]
player[+2],sound[-1]
TITLE: troubleshooting ad-2500 and ad-2600 no picture scrolling b/w . NR2
Variable: tag
i/p button[+2] NR2
dvd player[+1][p] NR2
player[+2],sound[-1] NR2
[{'contents': [{'sentence': 'repost from january 13 , 2004 with a better fit '
'title .',
'tag': ''},
{'sentence': 'im a more happier person after discovering the '
'i/p button ! ',
'tag': 'i/p button[+2]'},
{'sentence': 'it practically plays almost everything you give '
'it . ',
'tag': 'dvd player[+1][p]'},
{'sentence': "i 've had the player for about 2 years now and it "
'still performs nicely with the exception of an '
'occasional wwhhhrrr sound from the motor . ',
'tag': 'player[+2],sound[-1]'}],
'title': 'troubleshooting ad-2500 and ad-2600 no picture scrolling b/w . '},
{'contents': [{'sentence': 'repost from january 13 , 2004 with a better fit '
'title . NR2',
'tag': ''},
{'sentence': 'im a more happier person after discovering the '
'i/p button ! NR2',
'tag': 'i/p button[+2] NR2'},
{'sentence': 'it practically plays almost everything you give '
'it . NR2',
'tag': 'dvd player[+1][p] NR2'},
{'sentence': "i 've had the player for about 2 years now and it "
'still performs nicely with the exception of an '
'occasional wwhhhrrr sound from the motor . NR2',
'tag': 'player[+2],sound[-1] NR2'}],
'title': 'troubleshooting ad-2500 and ad-2600 no picture scrolling b/w . '
'NR2'}]

How do I use the if and elif with a random choice command?

I am trying to create a program to generate a complete random build in the NHL video games. Essentially you get an archetype and from that archetype you get certain abilities and such and i am trying to get the first part to run to just pick an archetype and a build. I also need to implement a way to do height, weight, and boosts as well, but I am unable to get the program to choose an ability based on whichever archetype that is chosen. This is the code I have so far and i know it is a lot and i dont know if this is the easiest way to do something like this because i have to make more if/elif loops for other areas of the build unless there is an easier more efficient way:
import random
allbuilds = ["two way forward", "power forward", "dangler", "sniper", "enforcer", "enforcer d"]
twfabils= ["truculence", "back at ya", "big rig"]
pwfabils= ["shutdown", "quick pick", "schnipe"]
dangabils= ["ankle breaker", "one tee", "puck on string"]
snipabils= ["one tee", "snappy", "schnipe"]
enfabils= ["heatseeker", "elite edges", "medic"]
enfdabils= ["unstoppable force", "stick em up", "ice pack"]
pickrandbuild=random.choice(allbuilds)
randbuild=print(pickrandbuild)
picktwfabil=random.choice(twfabils)
pickpwfabil=random.choice(pwfabils)
pickdangabil=random.choice(dangabils)
picksnipabil=random.choice(snipabils)
pickenfabil=random.choice(enfabils)
pickenfdabil=random.choice(enfdabils)
if randbuild == "two way forward":
print(picktwfabil)
elif randbuild == "power forward":
print(pickpwfabil)
elif randbuild == "dangler":
print(pickdangabil)
elif randbuild == "sniper":
print(picksnipabil)
elif randbuild == "enforcer":
print(pickenfabil)
else:
print(pickenfdabil)
If you format your data as a dictionary, you can reduce the coding to just two lines:
allbuilds = {
"two way forward": ["truculence", "back at ya", "big rig"],
"power forward": ["shutdown", "quick pick", "schnipe"],
"dangler": ["ankle breaker", "one tee", "puck on string"],
"sniper": ["one tee", "snappy", "schnipe"],
"enforcer": ["heatseeker", "elite edges", "medic"],
"enforcer d": ["unstoppable force", "stick em up", "ice pack"]
}
pickrandbuild = random.choice(list(allbuilds.keys()))
pickability = random.choice(allbuilds[pickrandbuild])
And, as was suggested in the comments on this answer, it can even be reduced to one line using tuple unpacking:
pickrandbuild, pickability = random.choice(list(allbuilds.items()))

Trying to understand .strip

I have a file, called input.py, which contains the following:
#This is a file of categories
Animals: cat, dog, frog, cheetah, tiger
Items: desk, chair, bus, cups, pencil
Technology: cellphone, TV, laptop, wifi-router
#That's the end of the file
and I want to filter out all blank lines and lines starting with # to produce output.py:
Animals: cat, dog, frog, cheetah, tiger
Items: desk, chair, bus, cups, pencil
Technology: cellphone, TV, laptop, wifi-router
My code is
with open('input.py', 'r') as infile, open('output.py', 'w') as outfile:
for line in infile:
if line[0].strip('') == '#' or line == '\n':
pass
else:
outfile.write(line)
but it doesn't strip off the line starting with a tab. I've tried replacing .strip('') with .strip('\t') but I get the same output:
Animals: cat, dog, frog, cheetah, tiger
Items: desk, chair, bus, cups, pencil
Technology: cellphone, TV, laptop, wifi-router
#That's the end of the file
Why is .strip('') or .strip('\t') not stripping off the tab?
Strip method can only remove characters from beginning or end of the string. Try using replace instead.
>>> line = "Animals: cat, dog, frog, cheetah, tiger"
>>> line.replace('\t', ' ')
'Animals: cat, dog, frog, cheetah, tiger'
You have forgotten to actually apply strip for applicable lines in your else condition. In addition, use str.startswith to test for whether your line begins with a specific string.
Here's a complete example:
from io import StringIO
mystr = StringIO("""Animals: cat, dog, frog, cheetah, tiger
Items: desk, chair, bus, cups, pencil
Technology: cellphone, TV, laptop, wifi-router
#That's the end of the file""")
with mystr as infile:
for line in infile:
if line.strip().startswith('#') or line == '\n':
pass
else:
print(line.strip())
Result:
Animals: cat, dog, frog, cheetah, tiger
Items: desk, chair, bus, cups, pencil
Technology: cellphone, TV, laptop, wifi-router

Python 3:random.choice command with if statments

I'm very new to python/programing, and, i've been playing with a small random choice generator. I'm really happy with how everything is working so far, but I'm having a problem with the if statement. how do I go about signaling that the if should point to the first generated choice from table Desert, and not generate a second choice? I've tried nesting the if statement in the print command, but got a lot of syntax errors. Here is the code:
import random
import sys
Name = ['Dave', 'Suzane', 'Jim', 'Diana']
Appitizers = ['Spinach & Artichoke Dip', 'Crispy Green Beans', 'Half Chicken Quesadila', 'None, for me, just the main course please']
MainCourse = ['Steak', 'Sea Food', 'Grilled Chicken', 'House Salad', 'Rib Sanwich', 'Soup of the Day']
Desert = ['Pie of the Day', 'Chocolate Moose', 'Cheeze Cake', "No Thanks, I'm Full!"]
Goodbye = ['Captian', 'Monsiure', 'Sir', 'Madame',]
Goodbye2 = ['Come back again!', 'See you soon', 'Thank you for stopping by', 'Thank you for choosing us!']
print("Hello ", random.choice(Name), "tonight you're meal will be:")
print("Appitizer:", random.choice(Appitizers))
print("Followed by Main Coure:", random.choice(MainCourse))
print("And finally Desert:", random.choice(Desert))
if random.choice(Desert)=="No Thanks, I'm Full!": print('Farwell!', random.choice(Goodbye1)), sys.exit()
print(random.choice(Goodbye2))
Thanks!
Assign the result of random.choice(Desert) to a variable, and use it in both places where it's applicable. (and while you're at it, change Goodbye1 to a variable that exists, such as Goodbye)
x = random.choice(Desert)
print("And finally Desert:", x)
if x=="No Thanks, I'm Full!":
print('Farwell!', random.choice(Goodbye))
sys.exit()

Categories