f = open("test.txt","w")
s= "This\nThis\nThis"
f.write(s)
f.close()
f= open("test.txt","r")
w=''
for i in f:
for j in i:
w = w+j
print(w)
print("Number of Characters",len(w))
print("Current Position of handler",f.tell())
f.close()
The output of the above is
This
This
This
Number of Characters 14
Current Position of handler 16
As per the file, there are 12 characters and 2 escape sequences so the number of characters is 14. I got it. But I did not get why the tell() function returns 17
Just an assumption.
I think, in your case, just for '\n', the pointer is moving twice. First, after reading the newline character i.e. \n, the pointer is moving one step right. Secondly, because of newline character, the pointer is going to the beginning of the next line. That's why an extra count is being added to tell() function's result. This won't be happen for other escape characters like '\t' etc.
I ran some examples on my system. You can notice the results one by one.
Example 1
f = open("test.txt","w")
s= "\t"
f.write(s)
f.close()
f= open("test.txt","r")
w=''
for i in f:
for j in i:
w = w+j
print(w)
print("Number of Characters",len(w))
print("Current Position of handler",f.tell())
f.close()
Output
>>>python .\test.py
Number of Characters 1
Current Position of handler 1
Example 2
f = open("test.txt","w")
s= "\n"
f.write(s)
f.close()
f= open("test.txt","r")
w=''
for i in f:
for j in i:
w = w+j
print(w)
print("Number of Characters",len(w))
print("Current Position of handler",f.tell())
f.close()
Output
>>>python .\test.py
Number of Characters 1
Current Position of handler 2
Example 3
f = open("test.txt","w")
s= "ThisThisThis"
f.write(s)
f.close()
f= open("test.txt","r")
w=''
for i in f:
for j in i:
w = w+j
print(w)
print("Number of Characters",len(w))
print("Current Position of handler",f.tell())
f.close()
Output
>>>python .\test.py
ThisThisThis
Number of Characters 12
Current Position of handler 12
Example 4
f = open("test.txt","w")
s= "ThisThisThis\n"
f.write(s)
f.close()
f= open("test.txt","r")
w=''
for i in f:
for j in i:
w = w+j
print(w)
print("Number of Characters",len(w))
print("Current Position of handler",f.tell())
f.close()
Output
>>>python .\test.py
ThisThisThis
Number of Characters 13
Current Position of handler 14
Example 5
f = open("test.txt","w")
s= "ThisThisThis\t"
f.write(s)
f.close()
f= open("test.txt","r")
w=''
for i in f:
for j in i:
w = w+j
print(w)
print("Number of Characters",len(w))
print("Current Position of handler",f.tell())
f.close()
Output
>>>python .\test.py
ThisThisThis
Number of Characters 13
Current Position of handler 13
For your case, you used \n two times in your string. You can count 2 instead of 1 for every \n while guessing the result of tell(). So, 4 + 2 + 4 + 2 + 4 = 16.
Related
I am making a program, that reads a .txt file and prints how many times a certain word has been used:
filename = 'for_python.txt'
with open(filename) as file:
contents = file.read().split()
dict = {}
for word in contents:
if word not in dict:
dict[word] = 1
else:
dict[word] += 1
dict = sorted(dict.items(), key=lambda x: x[1], reverse=True)
for i in dict:
print(i[0], i[1])
It works, but it treats words with commas as different words. Is there an easy and efficient way to solve this?
This is what I did.
filename = 'for_python.txt'
with open(filename) as file:
contents = file.read().splitlines()
dict = {}
for sentence in contents:
word_list = sentence.split(" ")
for word in word_list:
cleaned_word = " "
for character in word:
if character.isalnum():
cleaned_word += character
if cleaned_word not in dict:
dict[cleaned_word] = 1
else:
dict[cleaned_word] += 1
dict = sorted(dict.items(), key=lambda x: x[1], reverse=True)
for i in dict:
print(i[0], i[1])
def count_char(text, char):
count = 0
for c in text:
if c == char:
count += 1
return count
filename = input("Enter a filename: ")
with open(filename) as f:
text = f.read()
for char in "abcdefghijklmnopqrstuvwxyz":
perc = 100 * count_char(text, char) / len(text)
print("{0} - {1}%".format(char, round(perc, 2)))
It's a script that counts the relative occurrence of letters abcdefghijklmnopqrstuvwxyz in the given text file.
The first block defines a function that counts how many times the character char is present in the text:
def count_char(text, char):
count = 0
for c in text:
if c == char:
count += 1
return count
The second block asks you to input the name of the file:
filename = input("Enter a filename: ")
and saves the contents of that file as a string in the variable text:
with open(filename) as f:
text = f.read()
The third block displays the relative occurrence of characters a b c d e f g h i j k l m n o p q r s t u v w x y z in text.
For each of these characters, it first computes the proportion of the amount of the given characters in the text count_char(text, char) to the total length of the text len(text) and multiplies the result by 100 to convert it to percentage:
perc = 100 * count_char(text, char) / len(text)
and displays the results as a formatted string. The numbers in curly brackets are replaced by the character char and the percentage of its occurrence, rounded to two decimals round(perc, 2):
print("{0} - {1}%".format(char, round(perc, 2)))
You can read more about string formatting in Python here.
I'm writing a program that will count how many of each letter there are.
Currently, it's working but it counts upper and lower case letters separately. I tried to convert all of the characters to upper case but it didn't work.
myFile = open('textFile.txt', 'r+')
with open('textFile.txt', 'r') as fileinput:
for line in fileinput:
line = line.upper()
d = {}
for i in myFile.read():
d[i] = d.get(i,0) + 1
for k,v in sorted(d.items()):
print("{}: {}".format(k,v))
If my text file consists of:
abc
ABC
it will print:
(space) : 1
A: 1
B: 1
C: 1
a: 1
b: 1
c: 1
I would like it to print:
A: 2
B: 2
C: 2
the result of line = line.upper() is not used anywhere. Perhaps move the counting code into the block of code that performs the uppercase transformation. Then count the characters in each uppercased line.
in this you are changing character to upper case but reading file only
see line 4 , do somthing like this
myFile = open('textFile.txt', 'r+')
with open('textFile.txt', 'r') as fileinput:
for line in fileinput:
line = line.upper()
d = {}
#change is here
for i in line:
d[i] = d.get(i,0) + 1
for k,v in sorted(d.items()):
print("{}: {}".format(k,v))
In Python, indenting is critical, you are converting the input to uppercase, but then throwing it away.
Try rearranging it like this:
d = {}
#myFile = open('textFile.txt', 'r+') - removed as not needed due to "with" variant of file processing below.
with open('textFile.txt', 'r') as fileinput:
for line in fileinput:
line = line.upper()
for i in line:
d[i] = d.get(i,0) + 1
for k,v in sorted(d.items()):
print("{}: {}".format(k,v))
This will do it.
chars = []
with open('textFile.txt', 'r') as fileinput:
for line in fileinput:
for c in line:
chars.append(c.upper())
d = {}
for i in chars:
d[i] = d.get(i, 0) + 1
for k,v in sorted(d.items()):
print("{}: {}".format(k,v))
Or this:
d = {}
with open('textFile.txt', 'r') as fileinput:
for line in fileinput:
line = line.upper()
for i in line:
d[i] = d.get(i,0) + 1
for k,v in sorted(d.items()):
print("{}: {}".format(k,v))
I have a text file like this:
This is just
an example of
a textfile
and would like to find the sum of all words that don't contain an "e". This sum is to be printed for every line, and should be the total sum of words in that line.
Currently I have this:
with open(sys.argv[1], "r") as f:
count = 0
for line in f:
words = line.split()
for word in words:
if "e" not in word:
for char in word:
count += 1
print(count)
and the output I get is:
4
6
10
12
14
15
when it should be:
10
4
1
You can use the len builtin to get the length of a string. The reason you're getting larger numbers than you expect is that you're not resetting the count variable for each line, and also you're printing after every word, not each line.
with open(sys.argv[1], "r") as f:
for line in f:
count = 0
words = line.split()
for word in words:
if "e" not in word:
count += len(word)
print(count)
You can write this more compactly as
with open(sys.argv[1], "r") as f:
for line in f:
print(sum(len(word) for word in line.split() if 'e' not in word))
I have two data files (Amin file and Volume file). form the first one want to find out the number in front of a string "Amin". Then I want to open the second file and check all of the volume numbers and if they are smaller than Amin change that specific number to Amin.
Amin file looks like the following:
GP_DEF 1 4 "Zeitintervall Q_Strg [s]" 2 9.00000000e+002 0.00000000e+000 1
GP_DEF 1 5 "Hmin [m]" 2 1.00000000e-002 0.00000000e+000 1.79769313e+308
GP_DEF 1 6 "VELMAX [m/s]" 2 1.50000000e+001 0.00000000e+000 1
GP_DEF 1 7 "Amin" 2 0.5 0.5 0.5
Volume file looks like the following:
SCALAR
ND 6813
ST 0
TS 0.0
0.207
0.313
0.423
0.595
0.930
0.714
0.590
0.1
1.652
the result should be like the following:
SCALAR
ND 6813
ST 0
TS 0.0
0.5
0.5
0.5
0.595
0.930
0.714
0.590
0.5
1.652
I have written a code not in a pythonic way but logically should work. But it does not create a result. My code is as following:
with open("VOLUMEN.dat") as f1, open('V_korr.dat', 'w') as out:
mylist = f1.read().splitlines()[0:4]
print(mylist)
for item in mylist:
out.write("%s\n" % item)
with open('hydro_as-2d.2dm', 'r') as f, open('Amin.txt', 'a') as outfile:
for line in f:
if line.startswith('GP_DEF 1 7 "Amin" '):
try:
line = line.strip()
columns = line.split()
Amin = float(columns[4])
print("{:.2f}".format(Amin), file=outfile)
except ValueError:
pass
with open("VOLUMEN.dat") as f1, open('V_korr.dat', 'w') as out:
for line in f1:
if line.startswith('GP_DEF 1 7 "Amin" '):
try:
line = line.strip()
columns = line.split()
Vol = float(columns[0])
if (V<Amin):
print("{:.2f}".format(Amin), file=outfile)
else :
print(line,file=outfile)
except ValueError:
pass
Please give a hint, where did i do a mistake? Thanks!
I'm not going to try to untangle your code, but rather try to give a tentative solution to your somewhat unclear problem. Here is my suggestion:
#! /usr/bin/env python
#
def find_amin(fname, pattern, idx=5, default=None):
"""Locate first matching line in fname and return field at offset idx
If pattern is not found return default value.
"""
with open(fname) as fd:
for line in fd:
if line.startswith(pattern):
return line.split()[idx]
else:
return default
def adjust_volume_file(fname, limit, skip=3, indent=3):
"""Return lines in fname as a list adjusting data smaller than limit
Do not change the first skip lines. Adjusted numbers are output
with a margin of indent spaces.
"""
margin = indent * " "
result = []
with open(fname) as fd:
for idx, line in enumerate(fd):
if idx > skip:
result.append(margin + str(max(float(line), limit)) + '\n')
else:
result.append(line)
return result
if __name__ == "__main__":
amin = float(find_amin('amin-file.txt', ' GP_DEF 1 7 "Amin"'))
adjusted_data = adjust_volume_file('VOLUMEN.dat', amin)
with open('V_korr.dat', 'w') as fd:
fd.writelines(adjusted_data)