def word_counter(s)
word_list=s.split()
return len(word_list)
f=open("a.txt")
total=0
for i in f.readlines():
total+=word_counter(i)
print(total)
if I want to count number of alphabet(without blank), number of word used and average length of each 'a.txt', 'b.txt', 'c.txt', 'd.txt', 'e.txt'. At last, I want to get a 'total.txt' of all txt combined.
I dont know how to do more..
please help
You actually have the concept right. Just need to add a little more to reach your desired output.
Remember when you use f = open("a.txt"), make sure you call f.close(). Or, use the with keyword, like I did in the example. It automatically closes the file for you, even if you forget to.
I won't give the exact code as it is, but will provide the steps so that you learn the concepts.
Put all the .txt files names in a list.
Example, list_FileNames = ["a.txt", "b.txt"]
Then open each file, and get the entire file into a string.
for file in list_FileNames:
with open(file, 'r') as inFile:
myFileInOneString = inFile.read().replace('\n', '')
You have the right function to count words. For characters: len(myFileInOneString) - myFileInOneString.count(' ')
Save all these values into a varible and write to another file. Check how to write to a file: How to Write to a File in Python
Related
I'm new to programming so I'm pretty lost. I'm currently learning Python and I need to open a text file and change every letter to the next one in the alphabet (e.g a -> b, b -> c, etc.). How would I go about writing a code like this?
This sounds like a neat problem to work on for a beginner.
Things you may want to look at:
The open() function, which allows you to open files and read/write to them. For example
https://docs.python.org/3/library/functions.html#open
with open('test.out', 'r+') as fi:
all_lines = fi.readlines() # Read all lines from the file
fi.write('this string will be written to the file')
# The file is closed at this point in the code; `with()` is a context manager, look that up
The os.replace() function, which lets you overwrite one file with another. You might try reading the input file, writing to a new output file, then overwriting the input file with the new output file; this will let you do that.
https://docs.python.org/3/library/os.html#os.replace
Replacing a character with the next increment of a character is an interesting twist, as it's not something that a lot of python programmers have to deal with. Here's one way to increment a character:
x = 'c'
print(chr(ord(x) + 1)) # will print 'd'
Without just giving away the answer, this should give you the pieces that you need to get started, feel free to ask more questions.
I think that this will work very well. The code can be shortened I think but Im still not sure how. Not an expert with with open statements.
with open("(your text file path)", "r") as f:
data = f.readline()
new_data = ""
for x in range(len(data)):
i = ord(data[x][0])
i += 1
x = chr(i)
new_data += x
print(new_data)
with open("(your text file path)", "w") as f:
f.write(new_data)
You must change your letters to numbers so that you can increment them by one, and then change them back to letters. This should work.
I am trying to create leaderboard for my game. After the game ends, it asks for name and calculates score. However, when I write the Name and Score to the leaderboard.txt file and i try to import them back again, I cannot convert the number back to int from string. I need to convert it back to int so that i can sort the leaderboard by the most score achieved, or do I?
score = 5
name = "Adam"
file = open("leaderboard.txt", "a+")
file.write(name+" ")
file.write(str(score)+"\n")
file.close()
file = open("leaderboard.txt", "r")
list_of_lists = []
for line in file:
list_line = line.split()
list_of_lists.append(list_line)
for x in range(0,len(list_of_lists)):
if list_of_lists[x][1].isdigit == True:
x = int(x)
print(list_of_lists)
This does not work, the number stays string however no errors appear. I really dont know what I am missing. Is this even a right way of writing name and score to .txt file? Is there a better way to think about this? I am really desperate for help.
It's almost always best to do your transforms to the format most convenient for the remainder of your code as close to the ingress point as possible. Your code wants there to be a string and an integer. Do that conversion as you're reading the data in from the file, before adding it to the list_of_lists structure:
score = 5
name = "Adam"
file = open("leaderboard.txt", "a+")
file.write(name+" ")
file.write(str(score)+"\n")
file.close()
file = open("leaderboard.txt", "r")
list_of_lists = []
for line in file:
list_line = line.split()
list_line[1] = int(list_line[1]) # This is the new code
list_of_lists.append(list_line)
print(list_of_lists)
In so doing, your internal data structure contained in list_of_lists will contain exactly what the rest of the program needs; no need for subsequent transformations. You'll notice my example omits your second for loop; we handled the transform on initial read.
int is the new concept here. But by doing it within your file read loop, you avoid needing to do additional post-processing in a subsequent loop.
As a separate note, it's usually a good idea to have some idea of what you want to have happen if a line in your input file doesn't meet expectations; input validation. For example, you may want to quietly skip blank lines, and you may want to emit a warning to STDERR, and then skip overr any lines that don't follow the "string[space]integer" format.
The exact question to this problem is:
*Create a file with a 20 lines of text and name it “lines.txt”. Write a program to read this a file “lines.txt” and write the text to a new file, “numbered_lines.txt”, that will also have line numbers at the beginning of each line.
Example:
Input file: “lines.txt”
Line one
Line two
Expected output file:
1 Line one
2 Line two
I am stuck, and this is what I have so far. I am a true beginner to Python and my instructor does not make things very clear. Critique and help much appreciated.
file_object=open("lines.txt",'r')
for ln in file_object:
print(ln)
count=1
file_input=open("numbered_lines.txt",'w')
for Line in file_object:
print(count,' Line',(str))
count=+1
file_object.close
file_input.close
All I get for output is the .txt file I created stating lines 1-20. I am very stuck and honestly have very little idea about what I am doing. Thank you
You have all the right parts, and you're almost there:
When you do
for ln in file_object:
print(ln)
you've exhausted the contents of that file, and you won't be able to read them again, like you try to do later on.
Also, print does not write to a file, you want file_input.write(...)
This should fix all of that:
infile = open("lines.txt", 'r')
outfile = open("numbered_lines.txt", 'w')
line_number = 1
for line in infile:
outfile.write(str(line_number) + " " + line)
infile.close()
outfile.close()
However, here is a more pythonic way to do it:
with open("lines.txt") as infile, open("numbered_lines.txt", 'w') as outfile:
for i, line in enumerate(infile, 1):
outfile.write("{} {}".format(i, line))
Good first try, and with that, I can go through your code and explain what you did right (or wrong)
file_object=open("lines.txt",'r')
for ln in file_object:
print(ln)
This is fine, though generally you want to put a space before and after assignments (you are assigning the results of open to file_object) and add a space after a,` when separating arguments, so you might want to write that like so:
file_object = open("lines.txt", 'r')
for ln in file_object:
print(ln)
However, at this point the internal reference in the file_object have reached the end of the file, so if you wish to reuse the same object, you need to seek back to the beginning position, which is 0. As your assignment only states write to the file (and not on the screen), the above loop should be omitted from the file (but I get what you want to do, you want to see the contents of the file immediately though sometimes instructors are pretty strict on what they accept). Moving on:
count=1
file_input=open("numbered_lines.txt",'w')
for Line in file_object:
Looks pretty normal so far, again, minor formatting issues. In Python, typically we name all variables lower-case, as names with Capitalization are generally reserved for class names (if you wish to, you may read about them). Now we enter into the loop you got
print(count,' Line',(str))
This prints not quite what you want. as ' Line' is enclosed inside a quote, it is treated as a string literal - so it's treated literally as text and not code. Given that you had assigned Line, you want to take out the quotes. The (str) at the end simply just print out the string object and it definitely is not what you want. Also, you forgot to specify the file you want to print to. By default it will print to the screen, but you want to print it to the the numbered_lines.txt file which you had opened and assigned to file_input. We will correct this later.
count=+1
If you format this differently, you are assigning +1 to count. I am guessing you wanted to use the += operator to increment it. Remember this on your quiz/tests.
Finally:
file_object.close
file_input.close
They are meant to be called as functions, you need to invoke them by adding parentheses at the end with arguments, but as close takes no arguments, there will be nothing inside the parentheses. Putting everything together, the complete corrected code for your program should look like this
file_object = open("lines.txt", 'r')
count = 1
file_input = open("numbered_lines.txt", 'w')
for line in file_object:
print(count, line, file=file_input)
count += 1
file_object.close()
file_input.close()
Run the program. You will notice that there is an extra empty line between every line of text. This is because by default the print function adds a new line end character; the line you got from the file included a new-line character at the end (that's what make them lines, right?) so we don't have to add our own here. You can of course change it to an empty string. That line will look like this.
print(count, line, file=file_input, end='')
Naturally, other Python programmers will tell you that there are Pythonic ways, but you are just starting out, don't worry too much about them (although you can definitely pick up on this later and I highly encourage you to!)
The right way to open a file is using a with statement:
with open("lines.txt",'r') as file_object:
... # do something
That way, the context manager introduced by with will close your file at the end of "something " or in case of exception.
Of course, you can close the file yourself if you are not familiar with that. Not that close is a method: to call it you need parenthesis:
file_object.close()
See the chapter 7.2. Reading and Writing Files, in the official documentation.
In the first loop you're printing the contents of the input file. This means that the file contents have already been consumed when you get to the second loop. (Plus the assignment didn't ask you to print the file contents.)
In the second loop you're using print() instead of writing to a file. Try file_input.write(str(count) + " " + Line) (And file_input seems like a bad name for a file that you will be writing to.)
count=+1 sets count to +1, i.e. positive one. I think you meant count += 1 instead.
At the end of the program you're calling .close instead of .close(). The parentheses are important!
I am new to python and this may be a foolish question but it troubles me for several days.
I have about 30 log files, and each of them contains strings and data. They are almost the same except the difference of several data, and their names are arranged regularly, like 'log10.lammps', 'log20.lammps', etc.(The '10''20'represent the temperature of the simulation). I want to write a python script, which loops all these files and read their data in a specific line, (say line3900). Then I want to write these data to another data file, which is arranged like these:
10 XXX
20 XXX
30 XXX
.
.
.
I can read and write from a single file, but I cannot achieve the loop. Could anyone please tell me how to to that. Thanks a lot!
PS. Still another difficulty is that the data in line 3900 is presented like this: "The C11 is 180.1265465616", the one I want to extract is 180.1265465616. How can I extract the number without the strings?
This answer covers how to get all the files in a folder in Python. To summarize the top answer, to get all the files in a single folder, you would do this:
import os
import os.path
def get_files(folder_path):
return [f for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]
The next step would be to extract the number from the line The C11 is 180.1265465616.
I'm assuming that you have a function called get_line that given a filename, will return that exact line.
You can do one of three things. If the length of the number at the end is constant, then you can just grab the last n characters in the string and convert it to a number. Alternatively, you could split the string by the spaces and grab the last item -- the number. Finally, you could use Regex.
I'm just going to go with the second option since it looks the most straightforward for now.
def get_numbers():
numbers = []
for file in get_files('folder'):
line = get_line(file)
components = line.split(' ')
number = float(components[-1])
numbers.append(number)
return numbers
I wasn't sure how you wanted to write the numbers to the file, but hopefully these should help you get started.
#assuming files is a list of filenames
for filename in files:
with open(filename) as f:
<do stuff with file f>
ps. float(line.split(' ')[-1])
Well, I can give you a hint which path I would have taken (but there might be a better one):
Get all files in a directory to a list with os.listdir
Loop over every one and perform the following:
Use the re module to extract the temperature from the filename (if not match pattern break) else add it to a list (to_write_out)
Read the right line with linecache
Get the value (line.split()[-1])
Append the value to the list to_write_out.
Join the list to_write_out to a string with join
Write the string to a file.
Help with the regex
The regular expressions can be a bit tricky if you haven't used them before. To extract the temperature from your filenames (first bullet beneath point number 2) you would use something like:
for fname in filenames:
pattern = 'log(\d+)\.lammps'
match = re.search(pattern, fname)
if match:
temp = match.group(1)
# Append the temperature to the list.
else:
break
# Continue reading the right line etc.
Need your guidance!
Want to check some text file for any spelling mistakes against custom dictionary.
Here is the code:
Dictionary=set(open("dictionary.txt").read().split())
print Dictionary
SearchFile = open(input("sample.txt"))
WordList = set()
for line in SearchFile:
line = line.strip()
if line not in Dictionary:
WordList.add(line)
print(WordList)
But when I open and check back the sample file nothing changed. What Im doing wrong?
What you are doing wrong is not explicitly changing anything in any file.
Here is a little bit of code to show how to write stuff to files...
fp = open(somefilepath,'w')
this line opens a file for writing, the 'w' tells python to create the file if it does not exist, but also deletes the contents of the file if it does exist. If you want to open a file for writing and keep the current contents use 'a' instead. 'a' is for append.
fp.write(stuff)
writes whatever is in the variable 'stuff' to the file.
Hope this helps. For code more specific to your problem please tell us what exactly you want to write to your file.
Also, here is some documentation that should help you to better understand the topic of files: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
EDIT: but you are not changing anything!
By the end of your script here is what you have accomplished:
1. Dictionary is a set containing all acceptable words
2. WordList is a set containing all not acceptable lines
3. You have read to the end of SearchFile
If I am understanding your question correctly what you want to now do is:
4. find out which Disctionary word each line stored in Wordlist should be
5. re-write SearchFile with the offending lines replaced.
If this is correct, how do you intend to figure out which WordList entry is supposed to be which Dictionary entry? How do you know the actual corrections? Have you attempted this part of the script (it is the crux, after all. It would only be polite). Can you please share with us your attempt at this part.
Lets assume you have this function:
def magic(line,dictionary):
"""
this takes a line to be checked, and a set of acceptable words.
outputs what line is meant to be.
PLEASE tell us your approach to this bit
"""
if line in dictionary:
return line
...do stuff to find out which word is being mis spelt, return that word
Dictionary=set(open("dictionary.txt").read().split())
SearchFile = open("sample.txt",'r')
result_text = ''
for line in SearchFile:
result_text += magic(line.strip(),Dictionary) #add the correct line to the result we want to save
result_text += '\n'
SearchFile = open("sample.txt",'w')
SearchFile.write(result_text) # here we actually make some changes
If you have not thought about how to find the actual dictionary value that mis-spelt lines should be corrected to become, try this out: http://norvig.com/spell-correct.html
To re-iterate a previous point, it is important that you show that you have at least attempted to solve the crux of your problem if you want any meaningful help.