Read file as a list of tuples
One of the most efficient way to read delimited data like this is using numpy.genfromtxt
. For example
>>> import numpy as np
>>> np.genfromtxt(r't3.txt', delimiter=',')
array([[-34.968398, -6.487265],
[-34.969448, -6.48825 ],
[-34.967364, -6.49237 ],
[-34.965735, -6.582322]])
Otherwise you could use a list comprehension to read line by line, split on ','
, convert the values to float
, and finally produce a list of tuple
with open('t3.txt') as f:
mylist = [tuple(map(float, i.split(','))) for i in f]
Note that when you open a file using with
it will take care of closing itself afterwards so you don't have to.
Reading a list of tuples from a text file in python
You can write a reusable function which takes 2 parameters file_path
(on which you want to write tuple), tup
(which you want to append) and put your logic inside that. Later you can supply proper data to this function and it will do the job for you.
Note: Don't forget to read the documentation as comments in code
tuples.txt
(Before writing)
[('john', 'abc')]
Code
def add_tuple_to_file(file_path, tup):
with open(file_path, 'r+') as f:
content = f.read().strip() # read content from file and remove whitespaces around
tuples = eval(content) # convert string format tuple to original tuple object (not possible using json.loads())
tuples.append(tup) # append new tuple `tup` to the old list
f.seek(0) # After reading file, file pointer reaches to end of file so place it again at beginning
f.truncate() # truncate file (erase old content)
f.write(str(tuples)) # write back the updated list
# Try
add_tuple_to_file("./tuples.txt", ('jack', 'def'))
tuples.txt
(After writing back)
[('john', 'abc'), ('jack', 'def')]
References
- https://www.geeksforgeeks.org/python-ways-to-convert-string-to-json-object/
- How to open a file for both reading and writing?
Read file as a list of tuples of tuples
You can go one step further:
lst = [(5.0, 10.0, 6.4, 13.0), (7.0, 11.0, 5.6, 5.0), (4.0, 5.67, 3.1, 2.0), (13.0, 99.0, 3.2, 1.1)]
lst2 = [(tt[:2],tt[2:]) for tt in lst]
print(lst2)
Output
[((5.0, 10.0), (6.4, 13.0)), ((7.0, 11.0), (5.6, 5.0)), ((4.0, 5.67), (3.1, 2.0)), ((13.0, 99.0), (3.2, 1.1))]
Read tuples from text file
You can try regular expression too here:
import re
pattern='\((\d+,\d)\)'
with open('demo.txt','r') as f:
for line in f:
data=re.findall(pattern,line)
data_1=[]
for item in data:
data_1.append(tuple(map(lambda x:int(x),item.split(','))))
if data_1:
print(data_1)
output:
[(0, 0), (0, 0), (1, 0), (2, 3)]
[(1, 0), (1, 1), (1, 1), (3, 3)]
[(2, 0), (1, 2), (2, 1), (4, 4)]
[(3, 0), (2, 2), (3, 1), (5, 5)]
Or even better:
import re
pattern='\((\d+,\d)\)'
with open('demo.txt','r') as f:
for line in f:
data=re.findall(pattern,line)
data_1=[tuple(map(lambda x:int(x),item.split(','))) for item in data]
if data_1:
print(data_1)
Save a list of tuples to a file and read it again as a list
Use JSON all the way:
import json
lst = [(0, 'Paper 1.jpg'), (1, 'Paper 2.jpg'), (2, 'Paper 3.jpg'), (0, 'Paper 4.jpg'), (1, 'Paper 5.jpg'),
(2, 'Paper 6.jpg'), (0, 'Paper 7.jpg'), (1, 'Paper 8.jpg')]
# save as json
with open('data.json', 'w') as f:
json.dump(lst,f)
# read the file
with open('data.json') as f:
lst1 = [tuple(x) for x in json.load(f)]
print(f'lst1: {lst1}')
output
lst1: [(0, 'Paper 1.jpg'), (1, 'Paper 2.jpg'), (2, 'Paper 3.jpg'), (0, 'Paper 4.jpg'), (1, 'Paper 5.jpg'), (2, 'Paper 6.jpg'), (0, 'Paper 7.jpg'), (1, 'Paper 8.jpg')]
writing a file to a list of tuples
You can use .split()
to create the tuple out of each line and check the length:
def readDataPts(filename):
listPts = []
with open(filename, 'r') as f:
for line in f:
numbers = line.split()
if len(numbers) == 2:
listPts.append(map(int, numbers))
return listPts
Reading list of tuples from a file/saving data to a file
I will provide you a DIY solution, no external libraries, to give an idea of the workflow. The advises given in the comments are more the good but the underline principles should be "similar".
Performance, safety are not considered here, consider it just as a coding refresh for your learning path (or I hope so).
Requirements:
- use a unique separator symbol when writing to a file, it is easier when you will have read it in a second moment. I choose
;
but be free to change it (the,
gives conflicts with lists, & co) - when writing string objects "double double" quote them so that in the file they will be surrounded by doubles quotes (needed for
eval
see next step), so a string should be of the form'"Bio"'
- use
eval
to "resurrect" the string to an object - add write/read methods in the course class. In particular, the reader is a classmethod since it returns an class instance
class Course:
SEP = ';' # should be a unique character(s)
def __init__(self, name, credits_, grading_cats, assignments):
self.name, self.credits, self.grading_cats, self.assignments = self.data = name, credits_, grading_cats, assignments
def __str__(self): # not needed but useful to understand the difference with "write_file_formatter", see 2.
return f'{self.SEP}'.join( str(i) for i in self.data)
def write_file_formatter(self):
return f'{self.SEP}'.join( f'"{i}"' if isinstance(i, str) else str(i) for i in self.data)
@classmethod
def read_file_formatter(cls, course_string):
return cls(*map(eval, course_string.split(cls.SEP)))
# the courses
c1 = Course('Bio', 4, [('exam', 100)], [])
c2 = Course('cal', 4, [('exam', 50)], [])
print(c1.write_file_formatter())
# "Bio";4;[('exam', 100)];[]
print(c2.write_file_formatter())
# "cal";4;[('exam', 50)];[]
# simulate text file
courses_from_file = c1.write_file_formatter() + '\n' + c2.write_file_formatter()
# "Bio";4;[('exam', 100)];[]
# "cal";4;[('exam', 50)];[]
# simulate read from file
for course in courses_from_file.split('\n'):
c = Course.read_file_formatter(course)
print(type(c), c)
# <class '__main__.Course'> Bio;4;[('exam', 100)];[]
# <class '__main__.Course'> cal;4;[('exam', 50)];[]
Related Topics
Python Pandas .Isnull() Does Not Work on Nat in Object Dtype
Return Value from Python-Shell as Response
How to Run Linux Terminal Command in Python in New Terminal
How to Remove Hashtag, @User, Link of a Tweet Using Regular Expression
Json Dump in Python Writing Newline Character and Carriage Returns in File.
Finding the Most Frequent Character in a String
Regex to Remove Commas Before a Number in Python
Adding Different Sized/Shaped Displaced Numpy Matrices
Rotate Tick Labels for Seaborn Barplot
Loading and Parsing a Json File With Multiple Json Objects
Python: Plotting Percentage in Seaborn Bar Plot
How to Increase the Font Size of the Legend in My Seaborn Factorplot/Facetgrid
Opening a Word Document That Has a Password Using Docx Library
How to Check If Keras Is Using Gpu Version of Tensorflow
How to Eliminate Null Valued Cells from a CSV Dataset Using Python
How to Split Text Without Spaces into List of Words
How to Compile Python Script to Binary Executable
How to Stop a Running Function Without Exiting the Tkinter Window Entirely