Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I have a variable which its amount is changing every time. I like to know how can I find the line which contained the amount of my variable.
I am looking something like this:
but in this way it looks for the letter of "A"
how can I write a command which look for its amount("100")?
I tried this before
A = 100
with open ('my_file') as f:
for line in f :
if "A" in line:
print line
Putting quotes around something makes it a String. You want to actually reference the variable which contains your number, i.e. A instead of "A"
A = 100
with open('my_file') as f:
for line in f:
# str() converts an integer into a string for searching.
if str(A) in line:
print line
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
My current csv file :
'Date','Category','Ability'
'21,14,5','Sparrow','Air,land'
'4,5,6','Eagle','Air,Land'
'21,14,5','Penguin','water,land'
my code:
Living_beings=[]
with open(users_read,'r') as f:
reader=DictReader(f)
for row in reader:
if date.today().day in row['Date']:
Living_beings+=row['Category']
print(Living_beings)
Output ; ['S','p','a','r','r','o','w','P','e','n','g','u','i','n']
Expected output: [Sparrow, penguin]
I am not sure why it was split up...Any ideas on this.
The line Living_beings+=row['Category'] treats the contents of row['Category'] as a list of characters (because it is a string), and extends the Living_beings list with the row['Category'] list of characters.
Instead, you should use:
Living_beings.append(row['Category'])
Try Living_beings.append(row['Category']) instead.
My suspicion is the original code is treating row['Category'] as a list of individual characters so it can combine the lists.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 10 months ago.
Improve this question
This is the code i have used
file
I think you meant to read all the lines from the file and look at the last one. Like this:
try:
lines = myfile.readlines()
GetTopic = lines[-1].strip()
...
Your code read one line into lines and lines[-1] was a single character and GetTopic = print(lines[-1]) meant that GetTopic was None and so not equal to any of your literal strings.
readline() returns a string - the first line of the document.
readlines() returns an array, which is probably what you're expecting.
I prefer to use lines = [x.strip() for x in myfile.readlines()]
The result of assigning a print() is always None. Python isn't the command line.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to remove a " ' " character from a python string .
Below code gives a syntax error, How to achieve this task
final = string.replace(old_str, '\'', '')
To replace single quotes
final = old_str.replace("'", "")
str = "this is string example....wow!!! this is really string"
print(str.replace("is", "was"))
Here's an example of how to correctly use the replace function. This replaces all the is's with was's. It's hard to see what you're trying to do but use this as a guide.
You can do this
string = "some ' string"
final = string.replace('\'','')
print(final)
output
some string
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
i have txt file with one word in each line (library.txt) like:
Word1
Word2
Second txt file look like this:
I have got many words.
I know Word1 is very interesting.
ButWord2 is awful because its connected with another word.
I think there is no Word3 at all.
i need to search for those words from library.txt in Second txt file and replace them so those will look like this:
I have got many words.
For example <font=yellow>Word1</font> is very interesting.
But<font=yellow>Word2</font> is awful because its connected with another word.
I think there is no Word3 at all.
Ive got such code but it doesnt work:
rules =[]
with open(library, 'r') as libraryfile:
for line in libraryfile:
rules.append(line.rstrip())
with open(second', 'r') as secondfile:
with open(third', 'w') as thirdfile:
for line in secondfile:
if all(rule in line for rule in rules):
thirdfile.write(line.replace(rule, '<font color=yellow>'+rule+'</font>'))
else:
thirdfile.write(line)
First, you have to use any and not all. As you want at least one of them to be in rules to make changes.
Then rule is not defined outside of if, plus you might have several words from rules to replace in line, so best is to iterate over rules and replace when needed. This gives:
with open(second, 'r') as secondfile:
with open(third, 'w') as thirdfile:
for line in secondfile:
if any(rule in line for rule in rules):
for r in rules:
line = line.replace(r, '<font color=yellow>'+r+'</font>')
thirdfile.write(line)
You want any instead of all, as it is enough for one of the words from rules to appear in a line.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying read the contents of file through a variable
file = '/home/ec2-user/abc.txt'
paths = open("file","r+")
print paths.read()
But this is not working. I need help; I don't want specify 'abc.txt' directly.
First, you need to remove the quotes around "file":
paths = open(file,"r+")
When you're calling something using the value of a variable, you shouldn't quote the variable.
Second, if you don't want to hard code the path to file, you'll need to ask the user for it:
file = raw_input("Please enter the full path to the file: ")