[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
Related
I am reading a file in python and i need to add every line to a list. The problem is that every line is a list containing string and integers. This is the file i am reading:
file
this is the output of reading the file and putting it on the new list.
output
as you can see it is storing me the lines as a string but it need to be like this:
correct output
CODE:
def build_sets(f):
lista_valores = []
with open(f, 'r') as f:
for line in f:
lista_valores += line.strip().split('\n')
print(lista_valores)
#pass
Try this in your for loop:
lista_valores = line.strip('][').split(', ')
Also please only use english words to define your variables, it's one of the main rules of variable naming conventions.
Use ast.literal_eval with which you can safely evaluate an expression node or a string containing a Python literal or container display. Hence it will evluate your string as a proper list.
import ast
def build_sets(f):
lista_valores = []
with open(f, "r") as f:
a = f.read().splitlines()
for item in a:
lista_valores.append(ast.literal_eval(item))
print(lista_valores)
Output:
[[1, 'a', 'b'], [2, 'b', 'c']]
thank you guys, i figured it out this way:
lista_valores = []
with open(f, 'r') as f:
for line in f:
linha = line[0:len(line)-1].strip("[]").split(',')
lista_valores.append(linha)
for i in range(len(lista_valores)): # convert to int
for j in range(len(lista_valores[i])):
if lista_valores[i][j].isdigit():
lista_valores[i][j] = int(lista_valores[i][j])
If I store the tuple (1, 2, 3) in a txt file as a string, how could I extract it from the txt file and convert it back to the original tuple?
Maybe you could use
s="(1, 2, 3)"
tuple(int(x) for x in s.strip('()').split(','))
Remove the '(' and ')' and then use tuple().
Thanks to #bla for pointing out that s.strip('()') can be used instead of s.replace('(').replace(')').
if __name__ == "__main__":
# read it from a file
with open("filename.txt", "r") as myfile:
# loop through your text file line by line (if you have multiple of these)
lines = []
while True:
# read a line from text
line = myfile.readline()
# add the line to a list
lines.append(line)
# if there are no more lines to read, step out of loop
if not line:
break
# create a list of tuples
list_of_tup = []
for line in lines:
# remove next line tag "\n"
line = line[:-1]
# use eval() to evaluate a string
list_of_tup.append(eval(line))
If your file only contains the tuple, you could simply use eval.
In [1]: with open('tuple.txt') as f:
...: t = eval(f.read())
...:
In [2]: t
Out[2]: (1, 2, 3)
Note that eval should not be used with untrusted input! That means don't use eval on some random data that a user can enter or that your program has downloaded from the internet.
Creating tuple.txt is not very hard, assuming t references the tuple:
In [4]: with open('tuple.txt', 'w') as f:
...: f.write(str(t))
...:
In general though, I would advise you to use the json module whenever possible. It is a pretty general data format that is also easy for people to read. The latter is a good thing for generating test data and catching bugs.
If you have tabular data, then sqlite3 could be a good choice.
Let suppose your tuple is in MyTuple.txt then you can try below code written in ReadTuple.py to read tuple from a file.
» ReadTuple.py
# Open file in read mode
with open('MyTuple.json', encoding='utf-8') as f:
content = f.read()
t = eval(content);
# print tuple t read from file
print(t);
# print type of t
print(type(t));
» MyTuple.txt
Finally, if you will run above code using python ReadTuple.py then you will be able to see the read tuple and its type on your screen.
» Output
(1, 2, 3)
<class 'tuple'>
I have a text file data.txt that contains 2 rows of text.
first_row_1 first_row_2 first_row_3
second_row_1 second_row_2 second_row_3
I would like to read the second row of the text file and convert the contents into a list of string in python. The list should look like this;
txt_list_str=['second_row_1','second_row_2','second_row_3']
Here is my attempted code;
import csv
with open('data.txt', newline='') as f:
reader = csv.reader(f)
row1 = next(reader)
row2 = next(reader)
my_list = row2.split(" ")
I got the error AttributeError: 'list' object has no attribute 'split'
I am using python v3.
EDIT: Thanks for all the answers. I am sure all of them works. But can someone tell me what is wrong with my own attempted code? Thanks.
The reason your code doesn't work is you are trying to use split on a list, but it is meant to be used on a string. Therefore in your example you would use row2[0] to access the first element of the list.
my_list = row2[0].split(" ")
Alternatively, if you have access to the numpy library you can use loadtxt.
import numpy as np
f = np.loadtxt("data.txt", dtype=str, skiprows=1)
print (f)
# ['second_row_1' 'second_row_2' 'second_row_3']
The result of this is an array as opposed to a list. You could simply cast the array to a list if you require a list
print (list(f))
#['second_row_1', 'second_row_2', 'second_row_3']
Use read file method to open file.
E.g.
>>> fp = open('temp.txt')
Use file inbuilt generator to iterate lines by next method, and ignore first line.
>>> next(fp)
'first_row_1 first_row_2 first_row_3)\n'
Get second line in any variable.
>>> second_line = next(fp)
>>> second_line
'second_row_1 second_row_2 second_row_3'
Use Split string method to get items in list. split method take one or zero argument. if not given they split use white space for split.
>>> second_line.split()
['second_row_1', 'second_row_2', 'second_row_3']
And finally close the file.
fp.close()
Note: There are number of way to get respective output.
But you should attempt first as DavidG said in comment.
with open("file.txt", "r") as f:
next(f) # skipping first line; will work without this too
for line in f:
txt_list_str = line.split()
print(txt_list_str)
Output
['second_row_1', 'second_row_2', 'second_row_3']
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)
I have about 40 million lines of text to parse through and I want to treat each line as a split string and then ask for multiple slices (or subscripts, whatever they are called) using a list of numbers I generate in a method.
# ...
other_file = open('output.txt','w')
list = [1, 4, 5, 7, ...]
for line in open(input_file):
other_file.write(line.split(',')[i for i in list])
the subscript can't take this generator I have shown, but I want to ask the split line for multiple entries in it without having to iterate through the list in every line.
I apologize, I know this is a simple answer but I just can't think of it. It's so late!
CSV module can help you
import csv
reader = csv.reader(open(input_file, 'r'))
writer = csv.writer(open(output_file, 'w'))
fields = (1,4,5,7,...)
for row in reader:
writer.writerow([row[i] for i in fields])
For further improvements, open files with context managers
Don't use list as a variable name - remember there is a builtin called list
other_file = open('output.txt','w')
lst = [1,4,5,7,...]
for line in open(input_file):
fields = line.split(',')
other_file.write(",".join(fields[i] for i in lst) + "\n")
For further improvement use context managers to open/close the files for you
from operator import itemgetter
from csv import reader, writer
fields = 1,4,5,7
row_filter = itemgetter(*fields)
with open('inp.txt', 'r') as inp:
with open('out.txt', 'w') as out:
writer(out).writerows(map(row_filter, reader(inp)))