I have a text file called urldata.txt which I am opening and reading line by line. I wrote a for loop to read it line by line, but I want to save the output I receive as a list.
Here is what I have:
textdata = open("urldata.txt","r")
for line in textdata:
print(line)
this returns:
http://www.google.com
https://twitter.com/search?q=%23ASUcis355
https://github.com/asu-cis-355/course-info
I want to save these lines above as a list. Any suggestions?
I have tried appending and such, however, being new to Python I'm not sure how to go about this.
You just want a list of every line of the file?
urls = open("urldata.txt").read().splitlines()
If you just want the lines as a list, that's trivial:
with open("urldata.txt") as textdata:
lines = list(textdata)
If you want newlines stripped, use a list comprehension to do it:
with open("urldata.txt") as textdata:
lines = [line.rstrip('\r\n') for line in textdata]
Related
I have a couple of files inside a folder that I am trying to pull out text from, reason being that I eventually want to add this text into a newly created separate file. My tactic is to initialize a list and populate it with the text from each file, one by one. I have called this list myLines.
myLines = []
for line in f:
myLines.append(line)
for element in myLines:
f.write(myLines)
I get an error, and I know that it has something to do with .write() not accepting myLines because its a list rather than an argument. How would I go about turning the content of mylines into an acceptable argument for the write() method?
Thanks
IDK what's your intention of using myLines as the variable name. Given what you described it should be a list of texts, not a list of lines.
my_texts = []
# populate my_texts
for filename in input_files:
with open(filename) as f:
my_texts.append(f.read())
# write new file
with open('new_file_path.txt', 'w') as f:
f.write('\n'.join(my_texts))
assuming you want a new line separating texts from each file.
A more straightforward method would be to 1) open the output file in append mode (open('out_file_path', 'a')), and 2) read each input file in a loop, writing the content to the output file.
Try this -
myLines = []
for line in f:
myLines.append(line)
for element in myLines:
f.write(str(myLines))
Just convert myLines to string. You can use classes also if you want to preserve the type- list, but, that will be a bit lengthy.
I have been trying to split a file with text into distinct words.
I tried using the iter method, the nltk module and just splits, but something doesn't add when i am trying to append the outcome to a list.
Maybe there is some problem with the syntax of my approaching the file.
txt = open(game_file)
print txt.read()
names = []
linestream = iter(txt.read())
for line in linestream:
for word in line.split():
names.append(word)
when I try to print the list names, i just get '[]'.
Remove print txt.read(), you are iterating through empty opened file
Or make new variable text = txt.read() and do stuff with it
When you do txt.read() you're already at the end of your file. So when you try to restart it, the file pointer is already at the end and it does not find anything.
Try to delete your 2nd line and it should work!
Also, you don't need to do iter(txt.read()),
for line in txt should work!
Creating "iter" object of _any_file_obj_.read() returns iter object which iterates over every single character present in the file. Which is surely you dont want to acheive here as you want to split file text into distinct words.
If you want to get the every word form the text file, then you can follow the following approach.
word_list = []
txt = open(any_file) # creating file object
for line in txt.readlines():
if line:
[word_list.append(word) for word in line.split()]
txt.seek(0)
The last line txt.seek(0) is very important.
All this time, your code was giving empty list [] because the files current position after one full iteration was pointing at the end of file (EOF). _file_obj_.seek() can be used to return files current position to wherever you want in the opened file
I am reading a text file, separating the word and the number with the comma then adding them into separate lists however, the first name gets omitted. Here is my code.
for line in keywordFile:
line = keywordFile.readline()
keyword.append(line[0])
keywordValue.append(line[1])
You're jumping ahead with the first readline() and just use line defined in the for statement.
It seems that you keywordFile is a file object and since file objects are iterator (one shot iterables) after the first line that you loop over it you consume the first line.
for line in keywordFile:
^
And then you are using readline to read the next line which is extra here, so for getting ride of this problem you need to remove this part.
Also as a more pythonic way you can use a list comprehension to create the list of words by splitting the lines with comma.If you want to create a list of all words you can use a nested loop :
with open ('filename') as keywordFile :
words = [w for line in keywordFile for w in line.split(',')]
But if you want to put the separated words of each line in a separate list you just need to use a one loop :
with open ('filename') as keywordFile :
words = [line.split(',') for line in keywordFile]
Or as a better choice use csv module to open the file as a separated words.You can pass a delimiter argument to csv.reader function :
import csv
with open('file_name') as f:
words=csv.reader(f,delimiter=',')
Here words is a iterator from tuples of separated words. And of you want to concatenate them you can sue itertools.chain.from_iterable() function.
Try something like:
for line in keywordFile:
tokens = line.split(',')
keyword.append(tokens[0])
keywordValue.append(tokens[1])
I am new to python. I have a document that has one random word per line. There are thousands of words in this file. I am trying to print only the words that are four letters long. I tried this:
f=open("filename.txt")
Words=f.readlines()
for line in f:
if len(line)==4:
print(line)
f.close()
But python is blank when I do this. I am assuming I need to strip the blank spaces as well, but when I did
f.strip()
I received an error stating that .strip() doesn't apply to list items. Any help is grateful. Thanks!
'Python is blank' because you attempt to iterate over the file for a second time.
The first time is with readlines(), so when that iteration is finished you are at the end of the file. Then when you do for line in f you are already at the end of the file so there is nothing left over which to iterate. To fix this, drop the call to readlines().
To do what you want to have, I would just do this:
with open('filename.txt') as f:
for line in f: # No need for `readlines()`
word = line.strip() # Strip the line, not the file object.
if len(word) == 4:
print(word)
Your other error occurs with f.strip() because f is a file object- but you only strip a string. Therefore just split the line on each iteration as shown in the example above.
You should do:
for line in Words:
instead of
for line in f:
You want line.strip() because f is a file object, not a string.
I am having a txt file and I want to read its lines in python. Basically I am using the following method:
f = open(description,'r')
out = f.readlines()
for line in out:
line
What I want is to have access in every line of the text after the for loop. Thus, to store lines in a matrix or something list-like.
Instead of readlines you could use
lines = list(open(description, 'r'))
The opened file is an iterator, that yields lines. By calling list on it, you create a list of all of them. There's no real need to keep the open file around in a variable, doing it this way it will be closed.
But using readlines() to get a list is perfectly good as well.