Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have found the following code on the internet and it is meant to output the positions of the words in the list:
mylist
But it is not working, here is the code:
mylist="example string with spaces"
sentencelist=[]
for z in mylist.split(" "):
sentencelist.append(z)
wordlist=[]
for z in range(len(sentencelist)):
if sentencelist[z] not in wordlist:
wordlist.append(sentencelist[z])
wordpositions=[]
for i in range (len(sentencelist)):
for o in range(len(wordlist)):
if sentencelist[i]==wordlist[o]:
wordpositions.append(o+1)
wordlist=str(wordlist)
wordpositions=str(wordpositions)
inputFile=open("sentence.txt","w")
inputFile.write(wordlist)
inputFile.write("\n")
inputFile.write(wordpositions)
inputFile.close()
No error message comes out but it also doesn't work. Can someone expalin
For me, the script does successfully write a file sentence.txt with the content of wordlist and wordpositions.
If you want to have these printed out to console, as well, add:
print(wordlist)
print(wordpositions)
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm reading a csv file from my python code and expecting it to return a list of lists but I'm getting a list which contains a str. Can somebody tell me how I can convert it into a list thanks.
This is the code which is reading my csv file from my local directory.
with open('/home/developer/Desktop/csv/JE_csv/JP_Preisliste_GreenHome_Select_ab_29-10.2021.csv', encoding='utf-8-sig') as csv_data:
PriceListGetAG = list(csv.reader(csv_data, delimiter=';'))
for elem in range(0, len(GreenHomeSelect_PriceListGetAG), 1):
print(PriceListGetAG[elem])
This is what I'm getting.
["'14844', '26.10.2021', 'JP_C24_002', 'GreenHome Select', '01067'"]
This is what my expected output should be.
['14844', '26.10.2021', 'JP_C24_002', 'GreenHome Select', '01067']
I think your delimeter is comma. Change your parameter like this.
delimiter=','
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
the code is posted its giving me an error "str object cannot be interpreted as an integer" is there anyone who can fix ?
file = open(r'C:\Users\Raja Arsalan\Desktop\read_file.txt')
for i in file:
words= i.rstrip()
print("FRIST WORD" ,words)
letter = words.split("."," ")
print(letter)
import re
file = open(r'C:\Users\Raja Arsalan\Desktop\read_file.txt')
for i in file:
words= i.rstrip()
print("FRIST WORD" ,words)
#letter = words.split(".")#," ")
letter = re.split('. | ',words)
print(letter)
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
This should return the average of all of the values in the file. This is what I have. I get an error, could anyone help me out? Thank you!
def averageValueOnFile(fileName):
with open('fileName.txt') as f:
sum=0
count=0
for line in f:
count+=1
sum+=float(line.split(":")[1])
average=sum/count
print (average)
IndexError: list index out of range
What do the contents of the file look like?
One or more of your lines in the file does not have a : or is simply blank after the colon
Would sum+=float(line.split(":")[0]) work instead?
Your error likely comes from the fact that one of the lines in the file is not formatted as you expect: the split method returns a list with less than two elements.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Here is what I currently have, which is not working:
if "Forwarder" not in shp_name or "T_" not in shp_name or "Grad" not in shp_name:
I've also tried:
if ("Forwarder", "T_", "Grad") not in shp_name:
Samples of the input would be "DS_Forwarder_1" or "DS_Harvester_1". The script proceeds directly to else as it's unable to identify any of the above substrings in the primary string.
Try using the any built in.
if any(s in shp_name for s in ("Forwarder", "T_", "Grad")):
...
This will be true if any of the given strings are present in shp_name. You can use if not any(... if you want False when one of the strings is present.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am try to run this program as it start run but while running it's create error regrading CatchAllError . I want output as ' Pass ' But its not please help. Thanks
if line:
msg = url.strip()+' - CatchAllError'
print msg
with open("log_Error.txt", "a") as log:
log.write(msg+"\n")
else:
pass
You can see my whole program at https://ghostbin.com/paste/ypdmd.
You can see that if condition should be pass but it's not. To include target list go to https://ghostbin.com/paste/pjuox and download target list and other information.
You are mixing tabs and spaces. This confuses Python* and may cause unexpected behavior.
Use only tabs or only spaces, not both. Spaces is preferable.
(*well, not really. Python knows exactly how to handle tabs; it just does so in a way that is very surprising to most users. In particular, one tab is not equivalent to 4 spaces, or 8, or whatever it looks like in your text editor. See 2.1.8 - Indentation for more information.)