Count characters in each line of a file? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Any tips on how to count the amount of characters in each line of a text file, to then compare them using python?

It would be helpful to have an idea of what the end goal of your code is. What information do you want to gain from comparing the number of characters on a line? I would have written this as a comment, but it's not yet an option for me since I just joined.
If you're completely lost and don't know where to begin, here are some general bits of code to get you started (this is using Python 3.x):
file = open("YourFileName.txt", "r")
stringList = file.readlines()
The first line will open (read, hence the "r") the file in question. The second line of code goes through each line in the file and assigns them to a variable I called stringList. stringList is now a list, in which each element is a string corresponding to one line of your text file.
So,
print(stringList)
should return
['line0', 'line1', 'line2', 'line3', etc...]
It's possible that stringList could look like
['line0\n', 'line1\n', 'line2\n', 'line3\n', etc...]
depending on how your file is formatted. In case you didn't know, the '\n' is a newline character, equivalent to hitting enter on the keyboard.
From there you can create another list to hold the length of each line, and then loop through each element of stringList to store the lengths.
lengthList = []
for line in stringList:
lengthList.append(len(line))
len(line) takes the number of characters in a string and converts it to the equivalent integer value. Your lengthList will then contain how many characters are on each line, stored as ints. If there are '\n's, you may want to use len(line) - 1, depending on what you want to do with the lengths.
I hope this is helpful; I can't help with the comparisons until you provide some code and explain more specifically what you want to accomplish.

Related

sorting and printing multiple lines in a prompted file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Here is my existing code:
filename = input("Enter file name (with extension) to read: ")
if filename == 'x':
exit()
else:
c = open(filename, "r")
print("\nThe file,", filename, "opened successfully!")
print("The file", filename)
print(c.readlines())
c.close()
My output is the file I input, which is great! But my problem is that I just need to figure out how to print the list in sorted order. Since the list is generated by the user input, I've not had experience with sorting functions, would I sort the c.readlines directly?
Programming is all about decomposition, breaking large problems into smaller pieces. There are several distinct tasks in your program: prompt the user for a file name, open that file, sort the lines, and print them. You've already got a handle on the first two, so cast those out of your mind now and focus on the last two. Whether a file was hardcoded and inputted by the user makes no difference once you've got it open.
To sort a list you have to primary options: call l.sort() to sort it in place, or call sorted(l) to return a sorted copy without modifying the original. Let's use the second one and loop over the result of sorted():
for line in sorted(c.readlines()):
print(line)
The call to readlines() is optional. You can also loop over the file directly, which is shorthand for looping over the lines.
for line in sorted(c):
print(line)
(I prefer to call readlines() explicitly, myself. It's a stylistic choice; either way is acceptable.)

Ghematria in Python: hebrew letters to numbers and sum [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'd like to write a small program in Python that takes a hebrew text, converts each letter into its ghematria numbers, sum the numbers in each word, then prints the words as numbers.
to start off I was thinking to set the aleph beth variables,
like:
א=1
ב=2
ג=3
ד=4
and so on...
Keep in mind that since Hebrew letters are strong right-to-left characters, everything starts showing up right-to-left after one. Those assignments just look backward; they really are valid in Python 3.
Now that I have set the variables I was wondering how could I get the numbers for all the words from a txt file (unicode 8 of course)..
any help for a noob?
Keep in mind that since we are using hebrew the value of the variable is set at the LEFT of the = sign... and not at the right...
Python doesn't care. 1 = א won't work anway as you are trying to assign a value to an int.
The other way around also won't be a pleasure to work with. Unicode identifiers are supported only by Python 3, but I wouldn't go down that road.
What I would do is have a dictionary with letters as keys and numbers as values.
Then simply read the text file, convert each letter to its number and sum.
EDIT Something of this sort.
Note that I only have 3 letters in the dictionary because I'm lazy, and that I'm
using the dictionary's get() method so I won't get KeyError on spaces, new lines and punctuation marks.
vals = {'א': 1,
'ב': 2,
'ג': 3}
with open(path_to_file, encoding='utf-8') as f:
text = f.read()
sum = 0
for letter in text:
sum += vals.get(letter, 0)
print(sum)

File position and word splitter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a program that
Takes a text file with several sentences
Asks the user if they want to compress/decompress the file.
If Compress is chosen, the sentence will have all the unique words and the positions of these words needed to recreate the sentence again.
If decompress is chosen, the compressed text file will need to be found and using the position list given and the unique words - decompressed - the several sentences in the files need to be on separate lines.
Here is the code I have managed to create. It is a subroutine and it's rather faulty.
uniqueWords = []
positions = []
file =
def valChoice():
choice = (" ")
while choice not in ["compress", "decompress"]:
choice = input("Choose compress or decompress").lower()
if choice not in ["compress", "decompress"]:
print("Please input compress or decompress")
finalChoice = valChoice()
if finalChoice = ("compress"):
print("This where i get confused..")
elif finalChoice = ("decompress"):
print("This where i get confused..")
What is wrong with this code? How can I fix it?
With my caveat above, I'll take a shot at what I think you're asking.
To compress the file, iterate through the input words. Store each word reference in a dictionary: the word itself is the key, and its position is the value. If the word is already in the dictionary, then add the new position reference to the existing list of references.
Decompression works in reverse: make a sequence of positions and words. Sort that sequence into ascending order. Concatenate the words to make the original text.
Is that the level of help you need right now?

python 3 Acess file and check number of time phrase comes up [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to check a TSV of results and want to see how many times F and S come up (for failure or success) however I'm not sure how to doing this counting, or how to having it search the file
I've come across something that is sort of what I'm looking for: Python Program how to count number of times the letter c shows up
def countletter(S, F):
count = 0
for x in S:
if x.lower() == F.lower():
count += 1
return count
print (countletter("abcdefFf","F"))
But it isn't perfect and I'm not sure how to make it search the file.
Assuming that the count result applies to the whole file you can use a collections.Counter:
from collections import Counter
with open('input.tsv') as infile:
counts = Counter(infile.read())
for c in 'SF':
print '{}: {}'.format(c, counts.get(c))
This has the advantage of allowing you to obtain counts of any character (not just "S" and "F") with one pass of the file.
You could also just use str.count() for a specific character (or a string), but if you need counts more than one character you'll find a Counter more convenient and probably faster too.
You need to pass the file contents to your countletter function.
with open("FILE_TO_OPEN_AND_READ.txt") as f:
data = f.read()
print (countletter(data,"F"))
This opens and reads the file into data. For this example, I'm assuming your file is relatively small. Then data is passed into countletter as the first parameter, instead of a hardcoded string ("abcdefFf" in your example).
One note about your code, you are missing a closing parenthesis in your print statement. I've added that in my example above.

Python function,issue with input file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have function written by a colleague working in same field. So I know I should write script to execute python code,but issue of how the format of the input bbfile looks like bothers me.As I see fidlines read all the content,correct?My may concern is bbfile(tab delimited in my case),should it have three columns one for freq,other for breal and third for bimag?
def bbcalfunc(bbfile,nfreqlst):
fid=file(bbfile,'r')
fidlines=fid.readlines()
#define the delimiter
if bbfile.find('.txt')>=0:
delimiter='\t'
elif bbfile.find('.csv')>=0:
delimiter=','
freq=[]
breal=[]
bimag=[]
for ii in range(1,len(fidlines)):
linestr=fidlines[ii]
linestr=linestr.rstrip()
linelst=linestr.split(delimiter)
if len(linelst)>2:
freq.append(float(linelst[0]))
breal.append(float(linelst[1]))
bimag.append(float(linelst[2]))
else:
pass
freq=np.array(freq)
breal=np.array(breal)
bimag=np.array(bimag)
nfreq=np.log10(np.array(nfreqlst))
brinterp=interpolate.splrep(freq,breal)
brep=1E3*interpolate.splev(nfreq, brinterp)
biinterp=interpolate.splrep(freq,bimag)
bip=1E3*interpolate.splev(nfreq, biinterp)
return brep,bip
The format of the input file depends on the extension that you use, a .txt file will be a Tab Separated Values (tsv) file while a .csv file will be a Comma Separated Values (csv) file (please note that this is not a general convention, it is something that was decided by that colleague of yours that wrote the function, or maybe it's a local convention).
Each line of the file is usually composed by three {tab,comma} separated values, i.e., frequency, real part and imaginary part of a complex value.
I said usually composed because the code silently discards all the
lines for which the element count is less than three.
There is something here and there that can be streamlined in the code,
but it's inessential.
Rather, to answer your question re closing the file, change the first part
of the function to
def bbcalfunc(bbfile,nfreqlst):
#define the delimiter
if bbfile.find('.txt')>=0:
delimiter='\t'
elif bbfile.find('.csv')>=0:
delimiter=','
# slurp the file
with file(bbfile,'r') as fid:
fidlines=fid.readlines()
...

Categories