I have a file that has numbers like this in it:
5
10
15
20
I know how to write code that reads the file and inputs the numbers into a LIST but how do I write code that reads the file and inputs the number in a TUPLE if a tuple doesnt support the append function? this is what I got so far:
filename=input("Please enter the filename or path")
file=open(filename, 'r')
filecontents=file.readlines()
tuple1=tuple(filecontents)
print(tuple1)
the output is this:
('5\n', '10\n', '15\n', '20\n')
it should be this:
5,10,15,20
Try this:
s=','.join(map(str.rstrip,file))
Demo:
filename=input("Please enter the filename or path: ")
file=open(filename, 'r')
s=tuple(map(str.rstrip,file))
print(s)
Example output:
Please enter the filename or path: thefile.txt
(5,10,15,20)
Using with open(..) is recommended to make sure the file is closed once you are done with it. Then use an expression to transform the returned list to a tuple.
filename=input("Please enter the filename or path")
with open(filename, 'r') as f:
lines = f.readlines()
tup = tuple(line.rstrip('\n') for line in lines)
print(tup)
If you are sure they are integers, you can do something like:
filename=input("Please enter the filename or path")
with open(filename, 'r') as f:
lines = f.readlines()
result = tuple(int(line.strip('\n')) for line in lines)
print(resultt)
Also, if you have a list, you can always convert it to a tuple:
t = tuple([1,2,3,4])
So you can build the list appending elements, and finally convert it to a tuple
If you already know how to make a list of ints, just cast it to a tuple like what you are doing in your attempt to solve the problem.
Here a map object can also se casted to a tuple, but it also works with list:
filename=input("Please enter the filename or path: ")
with open(filename, 'r') as file:
filecontents=tuple(map(int, file.read().split()))
print(filecontents)
Also, if you use with statement you don't need to worry about closing the file (you was missing that part in your code too)
I have searched the web but I haven't found the answer for my problem:
I have a a dictionary with lists as elements and every list has a different length. For example like that:
dict_with_lists[value1] = [word1, word2, word3, word4]
dict_with_lists[value2] = [word1, word2, word3]
My main problem is, that I want to write the list elements in to a file which should be tab-separated and if the list is finished it should write the new list in a new line.
I found a solution like that:
with open('fname', 'w') as file:
file.writelines('\t'.join(i) + '\n' for i in nested_list)
But it doesn't only separate the words with tabs but also the characters.
If nested_list is one of your dictionary values, then you are applying '\t'.join() to the individual words. You'd want to join the whole list:
file.write('\t'.join(nested_list) + '\n')
or, if you were to loop over the values of the dictionary:
file.writelines(
'\t'.join(nested_list) + '\n'
for nested_list in dict_with_lists.values())
The above uses the file.writelines() method correctly; passing in an iterable of strings to write. If you were to pass in a single string, then you are only causing Python extra work as it loops over all the individual characters of that string to write those separately, after which the underlying buffer has to assemble those back into bigger strings again.
However, there is no need to re-invent the character-separated-values writing wheel here. Use the csv module, setting the delimiter to '\t':
import csv
with open('fname', 'w', newline='') as file:
writer = csv.writer(file, delimiter='\t')
writer.writerows(dict_with_lists.values())
The above writes all lists in the dict_with_lists dictionary to a file. The csv.writer() object doesn't mind if your lists are of differing lengths.
You need to turn each list value in the dictionary into a string of tab-separated values that also have a '\n' newline character at the end of each one of them:
value1, value2 = 'key1', 'key2'
dict_with_lists = {}
dict_with_lists[value1] = ['word1', 'word2', 'word3', 'word4']
dict_with_lists[value2] = ['word1', 'word2', 'word3']
fname = 'dict_list_values.tsv'
with open(fname, 'w') as file:
file.writelines(
'\t'.join(values)+'\n' for values in dict_with_lists.values()
)
I think you're doing a list comprehension over the list inside of the dictionary. An alternative solution would be
with open('fname', 'w') as file:
for nested_list in dict_with_lists.values():
for word in nested_list:
file.write(word + '\t')
file.write('\n')
\I'm just looping over the values of the dictionaries, which are lists in this case and joining them using a tab and writing a newline at the end of each list. I haven't tested it but theoretically I think it should work.
Instead of jumping on an answering your question I'm going to give you a hint on how to tackle your actual problem.
There is another way to store data like that (dictionaries of non-tabular form) and it is by saving it in the JSON-string format.
import json
with open('fname','w') as f:
json.dump(dict_with_lists, f)
And then the code to load it would be:
import json
with open('fname') as f:
dict_with_lists = json.load(f)
So I have a long text file with a bunch of numbers and I want to reformat this file so that every 12 characters are on their own line, the file is 4392 characters long. My strategy was to add the contents of the infile to a list and slice and append the first 12 characters to a new list then write it to an outfile using a while loop for the list slicing parameters. I am getting an error on out.writelines(l) :
TypeError: writelines() argument must be a sequence of strings.
Here is my code:
l = []
outl=[]
with open('r6.txt', 'r') as f, \
open('out.txt', 'w') as out:
outl.append(f)
a = 0
b = 11
while b <= 4392:
l.append(outl[a:b])
l.append('/n')
out.writelines(l)
a+=12
b+=12
l=[]
Well you're appending the file object to the list, and then you're taking slices of the list and writing them. Perhaps you forgot the file object reference among the strings.
Just use a print outl to get your answer. If you've got a file object among the items in the list, then you know :)
OR better yet:
l = []
outl=[]
with open('r6.txt', 'r') as f, \
open('out.txt', 'w') as out:
outl.extend(f.readlines())
a = 0
b = 11
while b <= 4392:
l.append(outl[a:b])
l.append('\n')
out.writelines(l)
a+=12
b+=12
l=[]
Hm, although other answers seem to be correct, I still think that the final solution can be, well, faster:
with open('r6.txt', 'r') as f, \
open('out.txt', 'w') as out:
# call anonymous lambda function returning f.read(12) until output is '', put output to part
for part in iter(lambda: f.read(12), ''):
# write this part and newline character
out.write(part)
out.write('\n')
Vlad-ardelean is correct in saying you need to append f.readlines() to outl instead of the file f.
Also, you're using writelines() to write a single line each time, but writelines() is intended for writing out a list of strings to a file, not one item lists. Perhaps a better way to approach the insertion of newline characters would be:
l = []
outl=[]
with open('r6.txt', 'r') as f, \
open('out.txt', 'w') as out:
# gets entire file as one string and removes line breaks
outl = ''.join(f.readlines()).replace('\n','')
l = [outl[each:each+12]+'\n' for each in xrange(0,len(outl),12)]
out.writelines(l)
Sample input for r6:
abcdefeounv lernbtlttb
berolinervio
bnrtopimrtynprymnpobm,t
2497839085gh
b640h846j048nm5gh0m8-9
2g395gm4-59m46bn
2vb-9mb5-9046m-b946m-b946mb-96m-05n=570n;rlgbm'dfb
output:
abcdefeounv
lernbtlttbbe
rolinerviobn
rtopimrtynpr
ymnpobm,t249
7839085ghb64
0h846j048nm5
gh0m8-92g395
gm4-59m46bn2
vb-9mb5-9046
m-b946m-b946
mb-96m-05n=5
70n;rlgbm'df
b
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 do I write a list to a file? writelines() doesn't insert newline characters, so I need to do:
f.writelines([f"{line}\n" for line in lines])
Use a loop:
with open('your_file.txt', 'w') as f:
for line in lines:
f.write(f"{line}\n")
For Python <3.6:
with open('your_file.txt', 'w') as f:
for line in lines:
f.write("%s\n" % line)
For Python 2, one may also use:
with open('your_file.txt', 'w') as f:
for line in lines:
print >> f, line
If you're keen on a single function call, at least remove the square brackets [], so that the strings to be printed get made one at a time (a genexp rather than a listcomp) -- no reason to take up all the memory required to materialize the whole list of strings.
What are you going to do with the file? Does this file exist for humans, or other programs with clear interoperability requirements?
If you are just trying to serialize a list to disk for later use by the same python app, you should be pickleing the list.
import pickle
with open('outfile', 'wb') as fp:
pickle.dump(itemlist, fp)
To read it back:
with open ('outfile', 'rb') as fp:
itemlist = pickle.load(fp)
Simpler is:
with open("outfile", "w") as outfile:
outfile.write("\n".join(itemlist))
To ensure that all items in the item list are strings, use a generator expression:
with open("outfile", "w") as outfile:
outfile.write("\n".join(str(item) for item in itemlist))
Remember that itemlist takes up memory, so take care about the memory consumption.
Using Python 3 and Python 2.6+ syntax:
with open(filepath, 'w') as file_handler:
for item in the_list:
file_handler.write("{}\n".format(item))
This is platform-independent. It also terminates the final line with a newline character, which is a UNIX best practice.
Starting with Python 3.6, "{}\n".format(item) can be replaced with an f-string: f"{item}\n".
Yet another way. Serialize to json using simplejson (included as json in python 2.6):
>>> import simplejson
>>> f = open('output.txt', 'w')
>>> simplejson.dump([1,2,3,4], f)
>>> f.close()
If you examine output.txt:
[1, 2, 3, 4]
This is useful because the syntax is pythonic, it's human readable, and it can be read by other programs in other languages.
I thought it would be interesting to explore the benefits of using a genexp, so here's my take.
The example in the question uses square brackets to create a temporary list, and so is equivalent to:
file.writelines( list( "%s\n" % item for item in list ) )
Which needlessly constructs a temporary list of all the lines that will be written out, this may consume significant amounts of memory depending on the size of your list and how verbose the output of str(item) is.
Drop the square brackets (equivalent to removing the wrapping list() call above) will instead pass a temporary generator to file.writelines():
file.writelines( "%s\n" % item for item in list )
This generator will create newline-terminated representation of your item objects on-demand (i.e. as they are written out). This is nice for a couple of reasons:
Memory overheads are small, even for very large lists
If str(item) is slow there's visible progress in the file as each item is processed
This avoids memory issues, such as:
In [1]: import os
In [2]: f = file(os.devnull, "w")
In [3]: %timeit f.writelines( "%s\n" % item for item in xrange(2**20) )
1 loops, best of 3: 385 ms per loop
In [4]: %timeit f.writelines( ["%s\n" % item for item in xrange(2**20)] )
ERROR: Internal Python error in the inspect module.
Below is the traceback from this internal error.
Traceback (most recent call last):
...
MemoryError
(I triggered this error by limiting Python's max. virtual memory to ~100MB with ulimit -v 102400).
Putting memory usage to one side, this method isn't actually any faster than the original:
In [4]: %timeit f.writelines( "%s\n" % item for item in xrange(2**20) )
1 loops, best of 3: 370 ms per loop
In [5]: %timeit f.writelines( ["%s\n" % item for item in xrange(2**20)] )
1 loops, best of 3: 360 ms per loop
(Python 2.6.2 on Linux)
Because i'm lazy....
import json
a = [1,2,3]
with open('test.txt', 'w') as f:
f.write(json.dumps(a))
#Now read the file back into a Python list object
with open('test.txt', 'r') as f:
a = json.loads(f.read())
Serialize list into text file with comma sepparated value
mylist = dir()
with open('filename.txt','w') as f:
f.write( ','.join( mylist ) )
In Python 3 you can use print and * for argument unpacking:
with open("fout.txt", "w") as fout:
print(*my_list, sep="\n", file=fout)
Simply:
with open("text.txt", 'w') as file:
file.write('\n'.join(yourList))
In General
Following is the syntax for writelines() method
fileObject.writelines( sequence )
Example
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "rw+")
seq = ["This is 6th line\n", "This is 7th line"]
# Write sequence of lines at the end of the file.
line = fo.writelines( seq )
# Close opend file
fo.close()
Reference
http://www.tutorialspoint.com/python/file_writelines.htm
file.write('\n'.join(list))
Using numpy.savetxt is also an option:
import numpy as np
np.savetxt('list.txt', list, delimiter="\n", fmt="%s")
You can also use the print function if you're on python3 as follows.
f = open("myfile.txt","wb")
print(mylist, file=f)
with open ("test.txt","w")as fp:
for line in list12:
fp.write(line+"\n")
Why don't you try
file.write(str(list))
I recently found Path to be useful. Helps me get around having to with open('file') as f and then writing to the file. Hope this becomes useful to someone :).
from pathlib import Path
import json
a = [[1,2,3],[4,5,6]]
# write
Path("file.json").write_text(json.dumps(a))
# read
json.loads(Path("file.json").read_text())
You can also go through following:
Example:
my_list=[1,2,3,4,5,"abc","def"]
with open('your_file.txt', 'w') as file:
for item in my_list:
file.write("%s\n" % item)
Output:
In your_file.txt items are saved like:
1
2
3
4
5
abc
def
Your script also saves as above.
Otherwise, you can use pickle
import pickle
my_list=[1,2,3,4,5,"abc","def"]
#to write
with open('your_file.txt', 'wb') as file:
pickle.dump(my_list, file)
#to read
with open ('your_file.txt', 'rb') as file:
Outlist = pickle.load(file)
print(Outlist)
Output:
[1, 2, 3, 4, 5, 'abc', 'def']
It save dump the list same as a list when we load it we able to read.
Also by simplejson possible same as above output
import simplejson as sj
my_list=[1,2,3,4,5,"abc","def"]
#To write
with open('your_file.txt', 'w') as file:
sj.dump(my_list, file)
#To save
with open('your_file.txt', 'r') as file:
mlist=sj.load(file)
print(mlist)
This logic will first convert the items in list to string(str). Sometimes the list contains a tuple like
alist = [(i12,tiger),
(113,lion)]
This logic will write to file each tuple in a new line. We can later use eval while loading each tuple when reading the file:
outfile = open('outfile.txt', 'w') # open a file in write mode
for item in list_to_persistence: # iterate over the list items
outfile.write(str(item) + '\n') # write to the file
outfile.close() # close the file
Another way of iterating and adding newline:
for item in items:
filewriter.write(f"{item}" + "\n")
In Python3 You Can use this loop
with open('your_file.txt', 'w') as f:
for item in list:
f.print("", item)
Redirecting stdout to a file might also be useful for this purpose:
from contextlib import redirect_stdout
with open('test.txt', 'w') as f:
with redirect_stdout(f):
for i in range(mylst.size):
print(mylst[i])
i suggest this solution .
with open('your_file.txt', 'w') as f:
list(map(lambda item : f.write("%s\n" % item),my_list))
Let avg be the list, then:
In [29]: a = n.array((avg))
In [31]: a.tofile('avgpoints.dat',sep='\n',dtype = '%f')
You can use %e or %s depending on your requirement.
i think you are looking for an answer like this.
f = open('output.txt','w')
list = [3, 15.2123, 118.3432, 98.2276, 118.0043]
f.write('a= {:>3d}, b= {:>8.4f}, c= {:>8.4f}, d= {:>8.4f}, e=
{:>8.4f}\n'.format(*list))
f.close()
poem = '''\
Programming is fun
When the work is done
if you wanna make your work also fun:
use Python!
'''
f = open('poem.txt', 'w') # open for 'w'riting
f.write(poem) # write text to file
f.close() # close the file
How It Works:
First, open a file by using the built-in open function and specifying the name of
the file and the mode in which we want to open the file. The mode can be a
read mode (’r’), write mode (’w’) or append mode (’a’). We can also specify
whether we are reading, writing, or appending in text mode (’t’) or binary
mode (’b’). There are actually many more modes available and help(open)
will give you more details about them. By default, open() considers the file to
be a ’t’ext file and opens it in ’r’ead mode.
In our example, we first open the file in write text mode and use the write
method of the file object to write to the file and then we finally close the file.
The above example is from the book "A Byte of Python" by Swaroop C H.
swaroopch.com