This question already has answers here:
What is the perfect counterpart in Python for "while not EOF" [duplicate]
(8 answers)
How to read user input until EOF in python?
(5 answers)
Closed 4 years ago.
in c / c++ i use EOF like
int n;
while( scanf("%d",&n) != EOF ){
printf("%d",n);
}
now how can i use EOF in Python ?
please give me the same code using python
You would do something like this in python:
with open(filename, 'r') as f:
for line in f:
print(line)
Related
This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Why doesn't the main() function run when I start a Python script? Where does the script start running (what is its entry point)?
(5 answers)
Closed 6 months ago.
My code goes through the compiler but doesn't replace what needs to be replaced in the new file. All files are in the same directory and I've tried running it as ./06_1 but nothing changed. Here is the code. Any help would be appreciated.
def replace_and_chars(fname):
f = open(fname,"r", encoding='windows-1255')
file_buffer_contents = f.read()
file_buffer_contents.replace("&","\\x26")
fout = open(fname.replace(".xml","") + "replaced.xml","w+")
fout.write(file_buffer_contents)
fout.close
f.close()
past = replace_and_chars("06_1.xml")
This question already has answers here:
"print" throws an invalid syntax error in Python 3
(4 answers)
Closed 2 years ago.
This is for Python 2:
import re
print sum([int(i) for i in re.findall('[0-9]+',open(raw_input('What is the file you want to analyze?\n'),'r').read())])
But why do I get a syntax error with Python 3?
Python3
import re
print sum([int(i) for i in re.findall('[0-9]+',open(input('What is the file you want to analyze?\n')).read())])
That is because in Python3 you should use brackets around the argument for the print function.
print()
so your code will work as soon as you write
print(sum([int(i) for i in re.findall('[0-9]+',open(input('What is the file you want to analyze?\n')).read())]))
This question already has answers here:
How to get part of string and pass it to other function in python?
(4 answers)
Closed 8 years ago.
A beginner question
My file looks like -->
10.5.5.81=apache,php,solr
10.5.5.100=oracle,coherence
How can I cut the IP part and store it into a list for further processing?
Please help.
answer = []
with open('path/to/file') as infile:
for line in infile:
answer.append(line.partition('=')[0].strip())
This question already has answers here:
Syntax error on print with Python 3 [duplicate]
(3 answers)
Closed 8 years ago.
#!/usr/bin/python
def creating_table():
mailingTable = open("mailingTable.txt", "r")
lines = mailingTable.readline()
for line in lines:
print line
mailingTable.close()
It says print line is invalid syntax. Why? I m using python 3.3.5
In Python 3.x, you have to write print() as follows, because now it's a function:
print(line)
In Python 2.x it was possible to omit the (), but that's no longer the case.
This question already has answers here:
How do I write a "tab" in Python?
(7 answers)
Closed 8 years ago.
Suppose I want to write "welcome" TAB "username" in file.
How can I specify this TAB?
f = open(filename, 'w')
f.write("welcome:"+TAB+"username");
Use \t character:
>>> print('\tsomething')
something
In your case that would be
f.write("welcome:\tusername")
Just:
f.write("welcome:\tusername")