So, I already have the code to get all the words with digits in them out of the text, now all I need to do is to have the text all in one line.
with open("lolpa.txt") as f:
for word in f.readline().split():
digits = [c for c in word if c.isdigit()]
if not digits:
print(word)
The split makes the words all be in a different column.
If I take out the .split(), it types in the words without the digits, literally just takes the digits out of the words, and makes every letter to be in a different column.
EDIT: Yes, print(word,end=" ") works, thanks. But I also want the script to now read only one line. It can’t read anything that is on line 2 or 3 etc.
The second problem is that the script reads only the FIRST line. So if the input in the first line would be
i li4ke l0ke like p0tatoes potatoes
300 bla-bla-bla 00bla-bla-0211
the output would be
i like potatoes
In Python v 3.x you’d use
to avoid the newline.
in Python v 2.x
you’d use the comma at the end of the items you are printing. Note that unlike in v3 you’d get a single blank space between consecutive prints
Note that
print(word),won’t prevent a newline in v 3.x.—
Update based on edit in original post re code problem:
With input:
this code:
will yield all the “words” that do not contain digits:
Note:
The post was a bit unclear if you wanted to process only the first line of the file, or if the problem was that your script only processed the first line. This solution can work either way depending on whether the
breakstatement is commented out or not.