Appending a document line by line to a list [duplicate] - python

This question already has answers here:
Python: AttributeError: '_io.TextIOWrapper' object has no attribute 'split'
(3 answers)
Closed 6 years ago.
I am trying to get each word in the text document "Payments" into a list. For each line of "Payments", I want it within a list in myList, so it would look something like this:
myList = [['E1234','12/09/14','440','A','0']['E3431','10/01/12','320','N','120']['E0987','04/12/16','342','A','137']]
I need to use .split() and .strip() in order to remove individual words from the commas and to remove the invisible next line space at the end of every line. This is my code that I have written so far:
myFile = open("Payments.txt")
myList = []
for line in myFile:
print(line.strip())
myList.append(line.strip())
myFile.close()
print(myList)
The program does work, but not in the way I was intending it to work. The program returned the following:
E1234,12/09/14,440,A,0
E3431,10/01/12,320,N,120
E0987,04/12/16,342,A,137
E5322,05/04/02,503,A,320
E9422,26/11/16,124,N,0
E7382,31/03/11,414,A,235
['E1234,12/09/14,440,A,0', 'E3431,10/01/12,320,N,120', 'E0987,04/12/16,342,A,137', 'E5322,05/04/02,503,A,320', 'E9422,26/11/16,124,N,0', 'E7382,31/03/11,414,A,235']
It has appended the document into myList, but it hasn't put each line into its' own list within myList and the line is a whole string, not individual strings, separated by a comma.
I did try adding .split(',') at the end of for line in myFile:, but it displayed an error message:
AttributeError: '_io.TextIOWrapper' object has no attribute 'split'

You need to call .split() on each line:
myList = []
for line in myFile:
print(line.strip())
myList.append(line.strip().split(","))
Or, in one line using a list comprehension:
myList = [line.strip().split(",") for line in myFile]
Or, you can also use a csv module:
import csv
with open("Payments.txt") as f:
reader = csv.reader(f)
myList = list(reader)
print(myList)

You can do everything you ask for - getting a list of lines from an open file, stripping each line, and splitting by , - all in a single line:
myList = map(lambda line: line.strip().split(","), myFile.readlines())

Try this:
myList = []
for line in myFile:
myList.append(line.strip().split(sep=','))
myFile.close()
print(myList)

The problem with your approach is that you didn't split the lines after you read them.
As a more pythonic way, you better to open your file with csv module that splits the lines with a specified delimiter, and returns an iterable of split lines.
import csv
with open("Payments.txt") as myFile:
spam_reader = csv.reader(myFile, delimiter=',')
print (list(spam_reader))

You need to call the .split() function in each line.strip(), as follows:
myFile = open("Payments.txt")
myList = []
for line in myFile:
print(line.strip())
myList.append(line.strip().split(","))
myFile.close()
print(myList)
The result will be the one you're requesting:
>>>[['E1234','12/09/14','440','A','0']['E3431','10/01/12','320','N','120']['E0987','04/12/16','342','A','137']]

Try this:
from pprint import pprint
with open("Payments.txt") as myFile:
myList = []
for line in myFile:
columns = line.strip().split(', ')
myList.append(columns)
pprint(myList)
Note the use of the context manager that automatically closes your file once you're done and the pretty print library that puts each item of your list on a different line. It makes it easier to read.

You used strip() instead of split(). Here is your original code modified to work:
myFile = open("Payments.txt")
myList = []
for line in myFile:
print(line.strip())
myList.append(line.split()) // We use split here
myFile.close()
print(myList)

Related

Python import list from text file as list

[2, 'hello', 3, 'good'] - is stored in myfile.txt on one line
with open(myfile,'r') as f:
myList = f.readlines()
but when I try to retrieve the first index, so "2' by using myList[0], the first square brackets is retrieved.
How can I set the imported line into a list?
use the ast module to convert the string to a list object
Ex:
import ast
with open(myfile,'r') as f:
data= f.read()
myList = ast.literal_eval(data)
.readlines() method reads lines of text from file, separated with the new line character and returns a list, containing those lines, so that's not the case.
You could have read the contents of the file and eval it like this:
with open(myfile,'r') as f:
my_list = eval(f.read())
Note, that the usage of eval is considered to be a really bad practice, as it allows to execute any python code written in the file.
You can use the json module for this (since a list is a valid json object):
import json
with open(myfile) as f:
myList = json.load(f)
You could do:
mylist = mylist[1:-1].split(",")
[1:-1] gets rid of brackets

Breaking txt file into list of lists by character and by row

I am just learning to code and am trying to take an input txt file and break into a list (by row) where each row's characters are elements of that list. For example if the file is:
abcde
fghij
klmno
I would like to create
[['a','b','c','d','e'], ['f','g','h','i','j'],['k','l','m','n','o']]
I have tried this, but the results aren't what I am looking for.
file = open('alpha.txt', 'r')
lst = []
for line in file:
lst.append(line.rstrip().split(','))
print(lst)
[['abcde', 'fghij', 'klmno']]
I also tried this, which is closer, but I don't know how to combine the two codes:
file = open('alpha.txt', 'r')
lst = []
for line in file:
for c in line:
lst.append(c)
print(lst)
['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o']
I tried to add the rstrip into the lst.append but it didn't work (or I didn't do it properly). Sorry - complete newbie here!
I should mention that I don't want newline characters included. Any help is much appreciated!
This is very simple. You have to use the list() constructor to make a string into its respective characters.
with open('alpha.txt', 'r') as file:
print([list(line)[:-1] for line in file.readlines()])
(The with open construct is just an idiom, so you don't have to do all the handling with the file like closing it, which you forgot to do)
If you want to split a string to it's charts you can just use list(s) (where s = 'asdf'):
file = open('alpha.txt', 'r')
lst = []
for line in file:
lst.append(list(line.strip()))
print(lst)
You are appending each entry to your original list. You want to create a new list for each line in your input, append to that list, and then append that list to your master list. For example,
file = open('alpha.txt', 'r')
lst = []
for line in file:
newLst = []
for c in line:
newLst.append(c)
lst.append(newLst)
print(lst)
use a nested list comprehension. The outer loop iterates over the lines in the file and the inner loop over the characters in the strings of each line.
with open('alpha.txt') as f:
out = [[char for char in line.strip()] for line in f]
req = [['a','b','c','d','e'], ['f','g','h','i','j'],['k','l','m','n','o']]
print(out == req)
prints
True

How to create a list from a text file in Python

I have a text file called "test", and I would like to create a list in Python and print it. I have the following code, but it does not print a list of words; it prints the whole document in one line.
file = open("test", 'r')
lines = file.readlines()
my_list = [line.split(' , ')for line in open ("test")]
print (my_list)
You could do
my_list = open("filename.txt").readlines()
When you do this:
file = open("test", 'r')
lines = file.readlines()
Lines is a list of lines. If you want to get a list of words for each line you can do:
list_word = []
for l in lines:
list_word.append(l.split(" "))
I believe you are trying to achieve something like this:
data = [word.split(',') for word in open("test", 'r').readlines()]
It would also help if you were to specify what type of text file you are trying to read as there are several modules(i.e. csv) that would produce the result in a much simpler way.
As pointed out, you may also strip a new line(depends on what line ending you are using) and you'll get something like this:
data = [word.strip('\n').split(',') for word in open("test", 'r').readlines()]
This produces a list of lines with a list of words.

python line separated values in a text when converted to list, adds "\n" to the elements in the list

I was astonished that a thing this simple has been troubling me. Below is the code
list = []
f = open("log.txt", "rb") # log.txt file has line separated values,
for i in f.readlines():
for value in i.split(" "):
list.append(value)
print list
The output is
['xx00', '\n', 'xx01in', '\n', 'xx01na', '\n', 'xx01oz', '\n', 'xx01uk', '\n']
How can I get rid of the new line i.e. '\n'?
list = []
f = open("log.txt", "rb") # log.txt file has line separated values,
for i in f.readlines():
for value in i.strip().split(" "):
list.append(value)
print list
.strip() removes trailing newlines. to be explicit you can use .strip('\n') or .strip('\r\n') in some cases.
you can read more about .strip() here
edit
better way to do what you wanted:
with open("log.txt", 'rb') as f:
mylist = [val for subl in [l.split(' ') for l in f.read().splitlines()] for val in subl]
for an answer which is much easier on the eyes, you can import itertools and use chain to flatten the list of lists, like #Jon Clements example
so it would look like this:
from itertools import chain
with open("log.txt", 'rb') as f:
mylist = list(chain.from_iterable(l.split(' ') for l in f.read().splitlines()))
If line-separated means that there is only one value per line, you don't need split() at all:
with open('log.txt', 'rb') as f:
mylist = map(str.strip, f)
In Python 3 wrap map() in a list().
with open("log.txt", "rb") as f:
mylist = f.read().splitlines()
Also, don't use list as a variable name, as it overshadows the python type list().
The correct way to do this, is:
with open('log.txt') as fin:
for line in fin:
print line.split()
By using split() without an argument, the '\n''s automatically don't become a problem (as split or split(None) uses different rules for splitting).
Or, more concisely:
from itertools import chain
with open('log.txt') as fin:
mylist = list(chain.from_iterable(line.split() for line in fin))
If you have a bunch of lines with space separated values, and you just want a list of all the values without caring about where the line breaks were (which appears to be the case from your example, since you're always appending to the same list regardless of what line you're on), then don't bother looping over lines. Just read the whole file as a single string and call split() with no arguments; it will split the string on any sequence of one or more whitespace characters, including both spaces and newlines, with the result that none of the values will contain any whitespace:
with open('log.txt', 'rb') as f:
values = f.read().split()

How to read a text file into a string variable and strip newlines?

I have a text file that looks like:
ABC
DEF
How can I read the file into a single-line string without newlines, in this case creating a string 'ABCDEF'?
For reading the file into a list of lines, but removing the trailing newline character from each line, see How to read a file without newlines?.
You could use:
with open('data.txt', 'r') as file:
data = file.read().replace('\n', '')
Or if the file content is guaranteed to be one-line
with open('data.txt', 'r') as file:
data = file.read().rstrip()
In Python 3.5 or later, using pathlib you can copy text file contents into a variable and close the file in one line:
from pathlib import Path
txt = Path('data.txt').read_text()
and then you can use str.replace to remove the newlines:
txt = txt.replace('\n', '')
You can read from a file in one line:
str = open('very_Important.txt', 'r').read()
Please note that this does not close the file explicitly.
CPython will close the file when it exits as part of the garbage collection.
But other python implementations won't. To write portable code, it is better to use with or close the file explicitly. Short is not always better. See https://stackoverflow.com/a/7396043/362951
To join all lines into a string and remove new lines, I normally use :
with open('t.txt') as f:
s = " ".join([l.rstrip("\n") for l in f])
with open("data.txt") as myfile:
data="".join(line.rstrip() for line in myfile)
join() will join a list of strings, and rstrip() with no arguments will trim whitespace, including newlines, from the end of strings.
This can be done using the read() method :
text_as_string = open('Your_Text_File.txt', 'r').read()
Or as the default mode itself is 'r' (read) so simply use,
text_as_string = open('Your_Text_File.txt').read()
I'm surprised nobody mentioned splitlines() yet.
with open ("data.txt", "r") as myfile:
data = myfile.read().splitlines()
Variable data is now a list that looks like this when printed:
['LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN', 'GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE']
Note there are no newlines (\n).
At that point, it sounds like you want to print back the lines to console, which you can achieve with a for loop:
for line in data:
print(line)
It's hard to tell exactly what you're after, but something like this should get you started:
with open ("data.txt", "r") as myfile:
data = ' '.join([line.replace('\n', '') for line in myfile.readlines()])
I have fiddled around with this for a while and have prefer to use use read in combination with rstrip. Without rstrip("\n"), Python adds a newline to the end of the string, which in most cases is not very useful.
with open("myfile.txt") as f:
file_content = f.read().rstrip("\n")
print(file_content)
Here are four codes for you to choose one:
with open("my_text_file.txt", "r") as file:
data = file.read().replace("\n", "")
or
with open("my_text_file.txt", "r") as file:
data = "".join(file.read().split("\n"))
or
with open("my_text_file.txt", "r") as file:
data = "".join(file.read().splitlines())
or
with open("my_text_file.txt", "r") as file:
data = "".join([line for line in file])
you can compress this into one into two lines of code!!!
content = open('filepath','r').read().replace('\n',' ')
print(content)
if your file reads:
hello how are you?
who are you?
blank blank
python output
hello how are you? who are you? blank blank
You can also strip each line and concatenate into a final string.
myfile = open("data.txt","r")
data = ""
lines = myfile.readlines()
for line in lines:
data = data + line.strip();
This would also work out just fine.
This is a one line, copy-pasteable solution that also closes the file object:
_ = open('data.txt', 'r'); data = _.read(); _.close()
f = open('data.txt','r')
string = ""
while 1:
line = f.readline()
if not line:break
string += line
f.close()
print(string)
python3: Google "list comprehension" if the square bracket syntax is new to you.
with open('data.txt') as f:
lines = [ line.strip('\n') for line in list(f) ]
Oneliner:
List: "".join([line.rstrip('\n') for line in open('file.txt')])
Generator: "".join((line.rstrip('\n') for line in open('file.txt')))
List is faster than generator but heavier on memory. Generators are slower than lists and is lighter for memory like iterating over lines. In case of "".join(), I think both should work well. .join() function should be removed to get list or generator respectively.
Note: close() / closing of file descriptor probably not needed
Have you tried this?
x = "yourfilename.txt"
y = open(x, 'r').read()
print(y)
To remove line breaks using Python you can use replace function of a string.
This example removes all 3 types of line breaks:
my_string = open('lala.json').read()
print(my_string)
my_string = my_string.replace("\r","").replace("\n","")
print(my_string)
Example file is:
{
"lala": "lulu",
"foo": "bar"
}
You can try it using this replay scenario:
https://repl.it/repls/AnnualJointHardware
I don't feel that anyone addressed the [ ] part of your question. When you read each line into your variable, because there were multiple lines before you replaced the \n with '' you ended up creating a list. If you have a variable of x and print it out just by
x
or print(x)
or str(x)
You will see the entire list with the brackets. If you call each element of the (array of sorts)
x[0]
then it omits the brackets. If you use the str() function you will see just the data and not the '' either.
str(x[0])
Maybe you could try this? I use this in my programs.
Data= open ('data.txt', 'r')
data = Data.readlines()
for i in range(len(data)):
data[i] = data[i].strip()+ ' '
data = ''.join(data).strip()
Regular expression works too:
import re
with open("depression.txt") as f:
l = re.split(' ', re.sub('\n',' ', f.read()))[:-1]
print (l)
['I', 'feel', 'empty', 'and', 'dead', 'inside']
with open('data.txt', 'r') as file:
data = [line.strip('\n') for line in file.readlines()]
data = ''.join(data)
from pathlib import Path
line_lst = Path("to/the/file.txt").read_text().splitlines()
Is the best way to get all the lines of a file, the '\n' are already stripped by the splitlines() (which smartly recognize win/mac/unix lines types).
But if nonetheless you want to strip each lines:
line_lst = [line.strip() for line in txt = Path("to/the/file.txt").read_text().splitlines()]
strip() was just a useful exemple, but you can process your line as you please.
At the end, you just want concatenated text ?
txt = ''.join(Path("to/the/file.txt").read_text().splitlines())
This works:
Change your file to:
LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE
Then:
file = open("file.txt")
line = file.read()
words = line.split()
This creates a list named words that equals:
['LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN', 'GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE']
That got rid of the "\n". To answer the part about the brackets getting in your way, just do this:
for word in words: # Assuming words is the list above
print word # Prints each word in file on a different line
Or:
print words[0] + ",", words[1] # Note that the "+" symbol indicates no spaces
#The comma not in parentheses indicates a space
This returns:
LLKKKKKKKKMMMMMMMMNNNNNNNNNNNNN, GGGGGGGGGHHHHHHHHHHHHHHHHHHHHEEEEEEEE
with open(player_name, 'r') as myfile:
data=myfile.readline()
list=data.split(" ")
word=list[0]
This code will help you to read the first line and then using the list and split option you can convert the first line word separated by space to be stored in a list.
Than you can easily access any word, or even store it in a string.
You can also do the same thing with using a for loop.
file = open("myfile.txt", "r")
lines = file.readlines()
str = '' #string declaration
for i in range(len(lines)):
str += lines[i].rstrip('\n') + ' '
print str
Try the following:
with open('data.txt', 'r') as myfile:
data = myfile.read()
sentences = data.split('\\n')
for sentence in sentences:
print(sentence)
Caution: It does not remove the \n. It is just for viewing the text as if there were no \n

Categories

Resources