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
myfile.write(Car_Number_plate+str' went at',+ Speed_In_Miles_Per_Hour+'/n')
I am getting an syntax error on python, I am trying to write to a text file.
Yes, you would. Here is valid syntax for what you are trying:
with open("file.txt", "w") as myfile:
myfile.write(Car_Number_plate + ' went at ' + Speed_In_Miles_Per_Hour + '/n')
To make it better, you probably meant to type \n for a line break, not /n and you probably want to make sure those variables are strings or don't use + to combine them. You should also make it more PEP8 friendly. So:
car_number_plate = "ABC123"
speed_in_miles_per_hour = 33
with open("file.txt", "w") as myfile:
myfile.write("{0} went at {1} mph \n".format(car_number_plate, speed_in_miles_per_hour))
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 4 years ago.
Improve this question
games=[]
file=open("egames.txt",'r')
for game in file:
games.append(game)
file.close()
print("All games made by Rockstar Games")
for game in games:
currentline=game.split(",")
publisher=currentline[5]
if publisher=="Rockstar Games":
print(currentline[0],currentline[1])
I dont get any errors i just get nothing being printed] with Rockstar Games.The actual Text file
Lines read from a file iterator end with newline characters. You should strip them as part of the normalization:
for game in file:
games.append(game.rstrip())
I'm guessing that the issue is the trailing newline characters, which are invisible to your eye. Try stripping off any white space:
publisher = currentline[5].strip()
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 4 years ago.
Improve this question
I've got an issue where my regex isn't parsing the output of a file I created:
#!/usr/bin/env python3
import wget, re
url=''
filename=wget.download(url)
with open ('Output.txt', "r") as f:
readlines=f.read()
ret=re.sub("^.*\^", "", readlines)
print(ret)
According to this site, the regex I'm using "^.*\^" is valid for my output. Sample output I'm feeding it is something like this:
1212-2010^readthispart
Where it has a carot for a delimiter. I tried double and single quotes to no avail and I'm not sure if it's an issue elsewhere in my code or what, but the printout does not match what I'm looking for. Ideas?
If I'm reading your question and edits right you're looking to return 'readthispart', correct? If so you need to look into using look-behinds in combination with search. See https://docs.python.org/2/library/re.html. re.search("(?<=\^).*",myinput)
You need to enable multiline mode:
re.sub('^.*\^', '', readlines, flags=re.MULTILINE)
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)
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.)