I am trying to copy the contents of a file that has MANY words and moving the contents into another file. The original file has 3 letter words that i'd like to sort out. unfortunately I have been unsuccessful in getting it to happen. I am newer to Python with some Java experience so im trying to do this pretty basic. Code is as follows:
# Files that were going to open
filename = 'words.txt'
file_two = 'new_words.txt'
# Variables were going to use in program
# Program Lists to transfer long words
words = []
# We open the file and store it into our list here
with open(filename, 'r') as file_object:
for line in file_object:
words.append(line.rstrip("\n"))
# We transfer the info into the new file
with open(file_two, 'a') as file:
x = int(0)
for x in words:
if len(words[x]) >= 5:
print(words[x])
file.write(words[x])
x += 1
I understand my problem is at the bottom while trying to import to the new file and perhaps a simple explanation might get me there, many thanks.
The problem is here:
with open(file_two, 'a') as file:
x = int(0)
for x in words:
if len(words[x]) >= 5:
print(words[x])
file.write(words[x])
x += 1
The reason for the error you're getting is that x isn't a number once the loop begins. It is a string.
I think you misunderstand how for loops work in python. They're more akin to foreach loops from other languages. When you do for x in words, x is given the value of the first element in words, then the second, and so on for each iteration. You however are trying to treat it like a normal for loop, going through the list by index. Of course this doesn't work.
There are two ways to go about fixing your code. You can either take the foreach approach:
with open(file_two, 'w') as file:
for x in words: #x is a word
if len(x) >= 5:
print(x)
file.write(x)
Or, use len() to loop through the range of indices of the list. This will yield behavior similar to that of a traditional for loop:
with open(file_two, 'a') as file:
for x in range(len(words)): #x is a number
if len(words[x]) >= 5:
print(words[x])
file.write(words[x])
There is also no need to manually increment x, or to give x an initial value, as it is reassigned at the beginning of the for loop.
Related
I am trying to assign the results of
x = 1
while (x < 11):
names =(random.choice(list(open('AllTickers.txt'))))
with open("NewStockList.txt", "w") as output:
for line in names:
output.write(str(names))
output.write('\n')
x = x + 1
to a new text doc.
When it ran, it just posted the same string 5 times.
That's a lot of code to refactor, first of all you need to declare how strings are separated. Get the variable from 'AllTickers.txt' out of loop for performance reasons and read it either with open or pathlib.Path.
If words or whatever you are checking is separated by linebreaks you can do .splitlines to the resulted text otherwise you might need to use split declaring the separator.
This will return you a list which can be randomly choose with random.choice.
Something like:
import pathlib
tickers = pathlib.Path('AllTickers.txt').read_text()
tickers = tickers.splitlines() # or `.split`
while a < b: # Your loop
ticker = random.choice(tickers)
# Now do whatever you need with that variable
What is the need for using both random.choice and a for loop.
because these two returns only a one item in each loop.
If you need get a random item from the AllStickers.txt and save it to NewStocklist.txt, then you need read every line in the Allsticker.txt and take a random item from the list and save it to newstocklist.txt.
Also you need to open newstockelist.txt from append mode, not in write mode. Otherwise, in your while loop the previously saved items will be overwritten in each turn.
import random
names = list(open("AllStickers.txt", "r").readlines())
x = 0
while x < 11:
with open("new.txt", "a") as output:
line = random.choice (names)
output.write(str(line))
x = x+1
continue
If you need to take items from AllSticker.txt and write them into the NewStockList.txt, then use a for loop and read every line in AllStickers.txt .
names = list(open("AllStickers.txt", "r").readlines())
x = 0
while x < 11:
with open("NewStockList.txt", "a") as output:
for line in names:
output.write(str(line))
x = x+1
continue
im trying to store each new line of a text file as a different list within a list, where the characters of that nested list are also individual cells. Right now it only appends the ending character of each line, not sure why due to the nested while loop. Anyone see the mistakes? Thanks
def read_lines(filename):
ls_1 = []
x = open(filename, 'r')
i = 0
t = 0
while True: #nested while loop to read lines and seperate lines into individual characters (cells)
read = x.readline()
if read == '':
break
st = read.strip("''\n''")
while t < len(st):
ls_2 = []
ls_2.append(st[t])
t += 1
ls_1.append(ls_2) #append a new list to the original list every time the while loop resets and a new line is read
#ls_2.clear() # removes contents so the next loop doesn't repeat the first readline (doesnt work for unkown reason)
t = 0 # resets the index of read so the next new line can be read from start of line
i += 1
x.close()
return ls_1
Whole txt file:
Baby on board, how I've adored
That sign on my car's windowpane.
Bounce in my step,
Loaded with pep,
'Cause I'm driving in the carpool lane.
Call me a square,
Friend, I don't care.
That little yellow sign can't be ignored.
I'm telling you it's mighty nice.
Each trip's a trip to paradise
With my baby on board!
The reason you are only getting the last character is because you create *a new list inside your inner loop:
while t < len(st):
ls_2 = []
ls_2.append(st[t])
t += 1
ls_1.append(ls_2)
Instead, you would have to do:
ls_2 = []
while t < len(st):
ls_2.append(st[t])
t += 1
ls_1.append(ls_2)
However, don't use while loops to read from files, file objects are iterators, so just use a for-loop. Similarly, don't use a while loop to iterate over a string.
Here is how you would do it, Pythonically:
result = []
with open(filename) as f:
for line in f:
result.append(list(line.strip()))
Or with a list comprehension:
with open(filename) as f:
result = [list(line.strip()) for line in f]
You almost never use while-loops in Python. Everything is iterator based.
I suggested you to use the function readlines from python, that way you can iterate of each line of the opened file, then you can cast the string to list, by doing that you generate a list with all characters that compose that string (which seems to be what you want).
Try using the following code:
def read_lines(filename):
x = open(filename, 'r')
ls_1 = [list(line.strip()) for line in x.readlines()]
x.close()
return ls_1
I have a file of more than 2000 jokes that I've done some preprocessing on and now have them in a list. I want to print them so that there are 20 per text document (except for the last document which may have fewer) and that they are numbered sequentially (jokeset1.txt, jokeset2.txt, etc), but when I try to do something like this, it only creates the first and last documents. How can I fix this?
i = 0
while len(jokes)>0:
i+=1
with open('Jokeset/jokeset' + str(i)+'.txt', 'w') as newfile:
if len(jokes)<20:
x = len(jokes)
else:
x = 20
random.shuffle(jokes)
for i in range(x):
print(jokes[i], file = newfile)
del jokes[:x]
You're rewriting your i variable inside of the loop!
You have i = 1 for the first run of the loop, so the first file is written, but then when you do
for i in range(x):
print(jokes[i], file = newfile)
the variable becomes i = 19. Then you just do i += 1 at the start of the outer loop and you're writing to a file called 'Jokeset/jokeset20.txt'. And this keeps happening over and over again, so no other files ever get written.
Simply rename one of your i variables and you will be good!
I want to be able to add a specific character into my file, using python code.
I have attempted using read functions, meaning lists, but those come up with an error of "TypeError: 'builtin_function_or_method"
I believe this means that python can not write a character into a specific place using the list function
Incorrect way:
file: 1)5
Code:
while true:
with open ('file','w') as f:
f.writeline[0]=+1
with open ('file','r') as f:
fc = f.read()
print (fc)
Expected output:
5,6,7,8,9,10,11,12,13,14,15....
I assumed that this line of code would increase the five until I stopped the program, but instead it sent the error code described earlier. Is there a way to write the code so that it does it as expected?
Basically, you would need to build a function to update a single character. I think this would work, but I literally wrote it in like three minutes, so take caution...
def write_at_index(filename,y_pos,x_pos,character):
"""Write a character 'character' at the given index"""
lines = 0 //begin lines outside scope of the with statement.
with open(filename,"r") as file:
lines = file.readlines()
if len(lines)<y_pos:
raise Exception('y pos out of bounds')
if len(lines[y_pos]) < x_pos
raise Exception('x_pos out of bounds')
lines[y_pos][x_pos] = character
with open(filename,"w") as file:
file.writelines(lines)
The first, your code will have an infinite loop:
while True: Do you have any check variable?
The second, I don't think this one can work for you: f.writeline[0]=+1
I'm not sure that my recommend code can help you fix your issue, but if it doesn't match your idea, please comment it.
check = True
# file = '1)5'
add_val = 5
while check:
open('file', 'w').write(add_val + ",")
add_val += 1
if add_val > 20: # condition to break the while loop
check = False
f = open('file','r').read()
print (f)
I have a Python script which I'm trying to use to print duplicate numbers in the Duplicate.txt file:
newList = set()
datafile = open ("Duplicate.txt", "r")
for i in datafile:
if datafile.count(i) >= 2:
newList.add(i)
datafile.close()
print(list(newList))
I'm getting the following error, could anyone help please?
AttributeError: '_io.TextIOWrapper' object has no attribute 'count'
The problem is exactly what it says: file objects don't know how to count anything. They're just iterators, not lists or strings or anything like that.
And part of the reason for that is that it would potentially be very slow to scan the whole file over and over like that.
If you really need to use count, you can put the lines into a list first. Lists are entirely in-memory, so it's not nearly as slow to scan them over and over, and they have a count method that does exactly what you're trying to do with it:
datafile = open ("Duplicate.txt", "r")
lines = list(datafile)
for i in lines:
if lines.count(i) >= 2:
newList.add(i)
datafile.close()
However, there's a much better solution: Just keep counts as you go along, and then keep the ones that are >= 2. In fact, you can write that in two lines:
counts = collections.Counter(datafile)
newList = {line for line, count in counts.items() if count >= 2}
But if it isn't clear to you why that works, you may want to do it more explicitly:
counts = collections.Counter()
for i in datafile:
counts[i] += 1
newList = set()
for line, count in counts.items():
if count >= 2:
newList.add(line)
Or, if you don't even understand the basics of Counter:
counts = {}
for i in datafile:
if i not in counts:
counts[i] = 1
else:
counts[i] += 1
The error in your code is trying to apply count on a file handle, not on a list.
Anyway, you don't need to count the elements, you just need to see if the element already has been seen in the file.
I'd suggest a marker set to note down which elements already occured.
seen = set()
result = set()
with open ("Duplicate.txt", "r") as datafile:
for i in datafile:
# you may turn i to a number here with: i = int(i)
if i in seen:
result.add(i) # data is already in seen: duplicate
else:
seen.add(i) # next time it occurs, we'll detect it
print(list(result)) # convert to list (maybe not needed, set is ok to print)
Your immediate error is because you're asking if datafile.count(i) and datafile is a file, which doesn't know how to count its contents.
Your question is not about how to solve the larger problem, but since I'm here:
Assuming Duplicate.txt contains numbers, one per line, I would probably read each line's contents into a list and then use a Counter to count the list's contents.
You are looking to use the list.count() method, instead you've mistakenly called it on a file object. Instead, lets read the file, split it's contents into a list, and then obtain the count of each item using the list.count() method.
# read the data from the file
with open ("Duplicate.txt", "r") as datafile:
datafile_data = datafile.read()
# split the file contents by whitespace and convert to list
datafile_data = datafile_data.split()
# build a dictionary mapping words to their counts
word_to_count = {}
unique_data = set(datafile_data)
for data in unique_data:
word_to_count[data] = datafile_data.count(data)
# populate our list of duplicates
all_duplicates = []
for x in word_to_count:
if word_to_count[x] > 2:
all_duplicates.append(x)