if a file contains A 2 B 3 , have to replace the user input if it contains an A or B with the values 2 and 3, (Example: A banana should turn to 2 banana) so far i have done like this:
word=input("Enter string: ")
word=list(word)
with open('mapping.txt') as f:
key = {}
for line in f:
first, second = line.split()
key[first] = second
for i in word:
if first in i:
word=word.replace(i,key[i])
but it does not change even does nott even prints, would u kindly Help Me
The reason it doesn't work is because each time you read the mapping.txt file, you create your dictionary, and at the same time you are checking the replacement words. So the first line from mapping will create one item in the dictionary, and then you check that one item against the string.
You also don't print anything.
You need to create your mapping once and then check the entire dictionary, like this:
mapping = {}
with open('mapping.txt') as f:
for line in f:
word, replacement = line.split()
mapping[word.strip()] = replacement.strip()
user_input = input("Enter string: ")
new_line = ' '.join(mapping.get(word, word) for word in user_input.split())
print(new_line)
When you run that, here is what you'll get:
Enter string: this is A string with a B
this is 2 string with a 3
I think this should work:
#!/usr/local/bin/python3
word=input("Enter string: ")
with open('input.txt') as f:
key = {}
for line in f:
first, second = line.split()
key[first] = second
for replacement in key:
word=word.replace(replacement,key[replacement])
print(word)
Related
EmpRecords=[1,'Angelo','Fabregas','South','City',
2,'Fabian','Fabregas','North','City',
3,'Griffin','De Leon','West','City',
4,'John','Doe','East','City',
5,'Jane','Doe','Southville','Town']
Output should something be like:
Enter word to search: Doe
Same words: 2
How do I do this? I should also clarify that EmpRecords is actually just a text File that is converted into a list.
so it's actually:
EmpRecords='''1,Angelo,Fabregas,South,City;
2,Fabian,Fabregas,North,City;
3,Griffin,De Leon,West,City;
4,John,Doe,East,City;
5,Jane,Doe',Southville,Town'''
Maybe this has something to do with finding the matching words?
Assuming you want to search for any word separated by comma and each line is a separate item:
Since your actual records are separated by ";" you need to create a nested list as below:
>>> record_groups = EmpRecords.split(";")
>>> final_groups = [each_group.split(",") for each_group in record_groups]
Later you can search through list items for the given word:
>>> word = "Doe"
>>> counter = 0
>>> for each_entry in final_groups:
if word in each_entry:
counter += 1
>>> print(counter)
APPROACH 2:
If it is already in a file you can directly open line by line and search:
word = "Doe"
counter = 0
with open("input.txt") as fd:
for line in fd:
if word in line.strip().split(",")
counter += 1
print(counter)
If you want to read from the file and count, you can use a loop.
import csv
with open('records.txt') as csvfile:
linereader = csv.reader(csvfile, delimiter=',')
count = 0;
target_value = 'Doe'
for row in linereader:
if row[2] == target_value:
count += 1;
print("Count: ",count)
You may need to remove the semicolon (;) from the last field if you will be using the data.
I have a text file with a list of words with a number and want to alter the list so instead of having the number next to the word, the word is printed that number of times.
So for example, with this list:
word, 2
for, 3
cat, 1
dog, 2
tiger, 1
I want it took look like this:
word
word
for
for
for
cat
dog
dog
tiger
For my python program I have this so far:
f = raw_input("Please enter a filename: ")
def openfile(f):
with open(f,'r') as a:
a = a.readlines()
b = [x.lower() for x in a]
return b
def fix(b):
newlist = []
for line in b:
split_line = line.split(',')
print openfile(f)
What I want to do is take the number and tell the program to print the word that number of times and then delete the number but I am not sure how to do that.
If you have any suggestions, answers, or need clarification please let me know!
Thanks
If you want to change the file itself you can use fileinput.input with inplace=True to change the file content:
import fileinput
import sys
for line in fileinput.input("in.txt",inplace=True):
if line.strip():
w,i = line.split(",")
sys.stdout.write("{}\n\n".format(w)*int(i))
Output:
word
word
for
for
for
cat
dog
dog
tiger
You should realised that print openfile(f) would only print b returned by openfile... I think what you wanna is the fixed-b.
Pythonic:
def fix(b):
for word, value in (x.split(',') for x in b):
print '\n'.join(word*int(value))
fix(openfile(f))
for line in b:
for i in range(int(line.split(", ")[1])):
print line.split(", ")[0]
You are in the right track, after spliting the line based on , convert the second element of split_line to int and then run a for loop for that much amount of times using range() function and then print the first element of split_line inside the for loop.
Also, some suggestions, you do not need to print openfile(f) , instead you should store that in a variable and then call the fix function on that variable. Also, if you want the new line in newlist and want to return the newlist , add the first element of split_line in newlist inside the for loop.
Example -
f = raw_input("Please enter a filename: ")
def openfile(f):
with open(f,'r') as a:
a = a.readlines()
b = [x.lower() for x in a]
return b
def fix(b):
newlist = []
for line in b:
split_line = line.split(',')
for _ in range(int(split_line[1])):
print split_line[0]
newlist.append(split_line[0])
b = openfile(f)
newb = fix(b)
You can do the whole thing with just this:
filename = raw_input("Please enter a filename: ")
with open(filename, 'r') as f:
lines = f.readlines()
for line in lines:
if line.strip() != '':
s = line.strip().split(',')
if len(s) > 1:
print "\n\n".join([s[0]] * int(s[1])) + "\n"
else:
print s[0] + "\n"
We can make a slight change to your fix function that will append each item the number of times required to the newlist
def fix(b):
newlist = []
for line in b:
split_line = line.split()
for i in range(int(split_line[1])):
newlist.append(split_line[0])
return '\n'.join(newlist)
Finally, we change your print statement at the bottom to be:
print fix(openfile(f))
To write this to a new file, simply open a file in writable mode and write to that.
outdata = fix(openfile(f))
with open('myfile.txt','w') as outfile:
outfile.write(outdata)
I am trying to write a python code that reads a bunch of lines from a text file and then splits the lines into words. Then for each word, it checks to see if the word is already present in the list, if not present then adds to it and finally sorts the list and prints the final list. This is what i have written so far.
fname = raw_input("Enter file name: ")
fh = open(fname)
new = list()
for line in fh:
line = line.rstrip()
word = line.split()
for w in word:
if w not in new:
final = new.append(w)
result = final.sort()
print result
But i am getting the below error..
AttributeError: 'NoneType' object has no attribute 'sort' on line 12
Don't know why? Any help?
Thanks
Upendra
fname = raw_input("Enter file name: ")
with open(fname) as fh: # use with to automatically close the file
final = []
new = [x.rstrip().split() for x in fh] # strip and split into sublists of words
for ele in new:
for word in ele: # for every word in each sublist
if not word in final: # if word not already in final then add word
final.append(word)
final.sort() # sort
print final
Both append() and sort() are in-place and don't return anything.
You can rewrite
final = new.append(w)
result = final.sort()
like so:
final = new + [w]
result = sorted(final)
Note that the sorting can (and should be) moved out of the loop: it's quite costly and doesn't need to be done for every word.
Finally, I'd suggest using sets instead of lists. They automatically ensure uniqueness, will result in simpler code, and are generally more performant for this sort of use case.
A sorted list of all words in a file:
fname = raw_input("Enter file name: ")
with open(fname) as fh:
result = sorted(set(fh.read().split()))
The error lies in the fact that the .append() method just appends to your existing list and does not return anything. So you should rewrite your code as:
fname = raw_input("Enter file name: ")
fh = open(fname)
new = list()
for line in fh:
line = line.rstrip()
word = line.split()
for w in word:
if w not in new:
new.append(w)
new.sort() #rather than sorting it in each iteration, why not just sort it at the end?
print new
Write a program that reads the contents of a random text file. The program should create a dictionary in which the keys are individual words found in the file and the values are the number of times each word appears.
How would I go about doing this?
def main():
c = 0
dic = {}
words = set()
inFile = open('text2', 'r')
for line in inFile:
line = line.strip()
line = line.replace('.', '')
line = line.replace(',', '')
line = line.replace("'", '') #strips the punctuation
line = line.replace('"', '')
line = line.replace(';', '')
line = line.replace('?', '')
line = line.replace(':', '')
words = line.split()
for x in words:
for y in words:
if x == y:
c += 1
dic[x] = c
print(dic)
print(words)
inFile.close()
main()
Sorry for the vague question. Never asked any questions here before. This is what I have so far. Also, this is the first ever programming I've done so I expect it to be pretty terrible.
with open('path/to/file') as infile:
# code goes here
That's how you open a file
for line in infile:
# code goes here
That's how you read a file line-by-line
line.strip().split()
That's how you split a line into (white-space separated) words.
some_dictionary['abcd']
That's how you access the key 'abcd' in some_dictionary.
Questions for you:
What does it mean if you can't access the key in a dictionary?
What error does that give you? Can you catch it with a try/except block?
How do you increment a value?
Is there some function that GETS a default value from a dict if the key doesn't exist?
For what it's worth, there's also a function that does almost exactly this, but since this is pretty obviously homework it won't fulfill your assignment requirements anyway. It's in the collections module. If you're interested, try and figure out what it is :)
There are at least three different approaches to add a new word to the dictionary and count the number of occurences in this file.
def add_element_check1(my_dict, elements):
for e in elements:
if e not in my_dict:
my_dict[e] = 1
else:
my_dict[e] += 1
def add_element_check2(my_dict, elements):
for e in elements:
if e not in my_dict:
my_dict[e] = 0
my_dict[e] += 1
def add_element_except(my_dict, elements):
for e in elements:
try:
my_dict[e] += 1
except KeyError:
my_dict[e] = 1
my_words = {}
with open('pathtomyfile.txt', r) as in_file:
for line in in_file:
words = [word.strip().lower() word in line.strip().split()]
add_element_check1(my_words, words)
#or add_element_check2(my_words, words)
#or add_element_except(my_words, words)
If you are wondering which is the fastest? The answer is: it depends. It depends on how often a given word might occur in the file. If a word does only occur (relatively) few times, the try-except would be the best choice in your case.
I have done some simple benchmarks here
This is a perfect job for the built in Python Collections class. From it, you can import Counter, which is a dictionary subclass made for just this.
How you want to process your data is up to you. One way to do this would be something like this
from collections import Counter
# Open your file and split by white spaces
with open("yourfile.txt","r") as infile:
textData = infile.read()
# Replace characters you don't want with empty strings
textData = textData.replace(".","")
textData = textData.replace(",","")
textList = textData.split(" ")
# Put your data into the counter container datatype
dic = Counter(textList)
# Print out the results
for key,value in dic.items():
print "Word: %s\n Count: %d\n" % (key,value)
Hope this helps!
Matt
I would like to define a function scaryDict() which takes one parameter (a textfile) and returns the words from the textfile in alphabetical order, basically produce a dictionary but does not print any one or two letter words.
Here is what I have so far...it isn't much but I don't know the next step
def scaryDict(fineName):
inFile = open(fileName,'r')
lines = inFile.read()
line = lines.split()
myDict = {}
for word in inFile:
myDict[words] = []
#I am not sure what goes between the line above and below
for x in lines:
print(word, end='\n')
You are doing fine till line = lines.split(). But your for loop must loop through the line array, not the inFile.
for word in line:
if len(word) > 2: # Make sure to check the word length!
myDict[word] = 'something'
I'm not sure what you want with the dictionary (maybe get the word count?), but once you have it, you can get the words you added to it by,
allWords = myDict.keys() # so allWords is now a list of words
And then you can sort allWords to get them in alphabetical order.
allWords.sort()
I would store all of the words into a set (to eliminate dups), then sort that set:
#!/usr/bin/python3
def scaryDict(fileName):
with open(fileName) as inFile:
return sorted(set(word
for line in inFile
for word in line.split()
if len(word) > 2))
scaryWords = scaryDict('frankenstein.txt')
print ('\n'.join(scaryWords))
Also keep in mind as of 2.5 the 'with' file contains an enter and exit methods which can prevent some issues (such as that file never getting closed)
with open(...) as f:
for line in f:
<do something with line>
Unique set
Sort the set
Now you can put it all together.
sorry that i am 3 years late : ) here is my version
def scaryDict():
infile = open('filename', 'r')
content = infile.read()
infile.close()
table = str.maketrans('.`/()|,\';!:"?=-', 15 * ' ')
content = content.translate(table)
words = content.split()
new_words = list()
for word in words:
if len(word) > 2:
new_words.append(word)
new_words = list(set(new_words))
new_words.sort()
for word in new_words:
print(word)