Python write complex nest list into file as it is - python

I trying to write complex nested list into file as it is,
so this is my list format
list=[[x number of elements],'a','b','c'],[y number of elements],'d',e',f']]
I am trying to write this in file as it is
[[x number of elements],'a','b','c'],[y number of elements],'d',e',f']]
So please help me out!

If you want to ensure the list can be read again, and you're sure it only contains simple Python types (lists, dictionaries, strings, and numbers), then you can do something like this:
import json
with open('output.txt', 'w') as out_file:
json.dump(your_list, out_file)
To pull it back into Python, you can do this:
import json
with open('output.txt', 'r') as in_file:
your_list = json.load(in_file)

Ignoring that your list is not correct one, you can do this with:
s = [[[1, 3, 4],'a','b','c'],[4, 5],'d','e','f']
text_file = open("Output.txt", "w")
text_file.write(str(s))
text_file.close()
Thus simply converting the list to a string and saving it.

Related

Writing and reading a list from a file in Python

I want to save a list in python to a file which should be able to read later and added to a list variable in later use.
As an example
list = [42,54,24,65]
This should be written to a file as
[42,54,24,65] or
list = [42,54,24,65]
And should be able to read later from python for a later use and assign it to a list variable
Right now I'm using the following code.
f = open('list_file', 'w')
f.write(values)
f.close()
This gives me an error
TypeError: write() argument must be str, not list
How can I fix this?
Thanks
You could do it also with pickle, it works similarly to json, but it can serialize a broader set of Python objects than json. Json serializes text, and is human readable, while pickle serializes bytes, not human readable.
Consider this example:
import pickle, json
list_ = [42,54,24,65]
with open('list_file.pickle', 'wb') as fp, open('list_file.json', 'w') as fj:
pickle.dump(list_, fp)
json.dump(list_, fj)
with open('list_file.pickle', 'rb') as fp, open('list_file.json', 'r') as fj:
list_unpickled = pickle.load(fp)
list_from_json = json.load(fj)
print(list_unpickled) #[42, 54, 24, 65]
print(list_from_json) #[42, 54, 24, 65]
Notice that with pickle you have to open the files with the 'b' for binary reading/writing.
A side note: do not use variables with the same name as python keywords, like list.
According to 12.1.4 in the documentation:
The following types can be pickled:
None, True, and False
integers, floating point numbers, complex numbers
strings, bytes, bytearrays
tuples, lists, sets, and dictionaries containing only picklable objects
functions defined at the top level of a module (using def, not lambda)
built-in functions defined at the top level of a module
classes that are defined at the top level of a module
instances of such classes whose dict or the result of calling getstate() is picklable (see section Pickling Class Instances for details).
If you just have a simple list, then you can use JSON and the json module.
import json
data = [42,54,24,65]
with open('output.txt', 'w') as f_out:
json.dump(data, f_out)
with open('output.txt', 'r') as f_in:
data2 = json.load(f_in)
print(data2) # [42,54,24,65]
And the contents of output.txt looks like
[42,54,24,65]
Map all values in the list to strings first, the write method only supports strings.
E.g. list = list(map(str, list))
Also calling a variable "list" is a bad practice, use something like "ls" or whatever differs from standard Python keywords. If you want to use it later, you can just delimit the values using spaces. Just write it like f.write(" ".join(list)). Then, to read it back into a list, do list = f.readline().split() This, however, will keep the values in the list as strings, to get them back to ints, map again like list = list(map(int, list))
According to the error in your code you passing a list to f.write().you need to pass string.
I assuming you want to write one word per line.try the code below it should work.
f = open('list_file', 'w')
for value in list:
f.write(value+"\n")
f.close()
To read later you can just open file again and read using this code:
f = open('list_file', 'r')
for line in f:
print line.strip()
f.close()
Turning my comment into an answer:
Try Saving and loading objects and using pickle:
import pickle
filehandler = open(b"Fruits.obj","wb")
pickle.dump(banana,filehandler)
To load the data, use:
file = open("Fruits.obj",'r')
object_file = pickle.load(file)

New to python and I need to write a variable to a file [duplicate]

This is a slightly weird request but I am looking for a way to write a list to file and then read it back some other time.
I have no way to remake the lists so that they are correctly formed/formatted as the example below shows.
My lists have data like the following:
test
data
here
this
is one
group :)
test
data
here
this
is another
group :)
If you don't need it to be human-readable/editable, the easiest solution is to just use pickle.
To write:
with open(the_filename, 'wb') as f:
pickle.dump(my_list, f)
To read:
with open(the_filename, 'rb') as f:
my_list = pickle.load(f)
If you do need them to be human-readable, we need more information.
If my_list is guaranteed to be a list of strings with no embedded newlines, just write them one per line:
with open(the_filename, 'w') as f:
for s in my_list:
f.write(s + '\n')
with open(the_filename, 'r') as f:
my_list = [line.rstrip('\n') for line in f]
If they're Unicode strings rather than byte strings, you'll want to encode them. (Or, worse, if they're byte strings, but not necessarily in the same encoding as your system default.)
If they might have newlines, or non-printable characters, etc., you can use escaping or quoting. Python has a variety of different kinds of escaping built into the stdlib.
Let's use unicode-escape here to solve both of the above problems at once:
with open(the_filename, 'w') as f:
for s in my_list:
f.write((s + u'\n').encode('unicode-escape'))
with open(the_filename, 'r') as f:
my_list = [line.decode('unicode-escape').rstrip(u'\n') for line in f]
You can also use the 3.x-style solution in 2.x, with either the codecs module or the io module:*
import io
with io.open(the_filename, 'w', encoding='unicode-escape') as f:
f.writelines(line + u'\n' for line in my_list)
with open(the_filename, 'r') as f:
my_list = [line.rstrip(u'\n') for line in f]
* TOOWTDI, so which is the one obvious way? It depends… For the short version: if you need to work with Python versions before 2.6, use codecs; if not, use io.
As long as your file has consistent formatting (i.e. line-breaks), this is easy with just basic file IO and string operations:
with open('my_file.txt', 'rU') as in_file:
data = in_file.read().split('\n')
That will store your data file as a list of items, one per line. To then put it into a file, you would do the opposite:
with open('new_file.txt', 'w') as out_file:
out_file.write('\n'.join(data)) # This will create a string with all of the items in data separated by new-line characters
Hopefully that fits what you're looking for.
Let's define a list first:
lst=[1,2,3]
You can directly write your list to a file:
f=open("filename.txt","w")
f.write(str(lst))
f.close()
To read your list from text file first you read the file and store in a variable:
f=open("filename.txt","r")
lst=f.read()
f.close()
The type of variable lst is of course string. You can convert this string into array using eval function.
lst=eval(lst)

How can I read a file that contains a list of dictionaries into python?

I've created a file that contains a list of dictionaries that I was working with. Unfortunately, I'm not sure how to re-import that file back into python in that same format.
I initially wrote the file out as JSON and as text, like this:
d = list_of_dics
jsonarray = json.dumps(d)
with open('list_of_dics.txt', 'w') as outfile:
json.dump(jsonarray, outfile)
with open('list_of_dics.json', 'w') as outfile:
json.dump(jsonarray, outfile)
Can anyone suggest a way to re-import these into python in the same format — i.e., a list of dictionaries?
You're using json.dump() incorrectly. You should be passing d to it directly, not the output of json.dumps(d). Once you do that, you can use json.load() to retrieve your data.
with open('list_of_dics.txt', 'r') as infile:
d = json.load(infile)
With
json.dumps(d)
you've (JSON-)encoded list d in a string (which you assign to a variable misleadingly called jsonarray).
With
json.dump(jsonarray, outfile)
you've JSON-encoded that string and written the result to outfile.
So it's now (unnecessarily) doubly JSON-encoded in the files list_of_dics.txt and list_of_dics.json.
To cleanly get it back from there (without resorting to manual string manipulation) you have to decode it twice:
import json
with open('list_of_dics.json', 'r') as infile:
recovered_d = json.loads(json.load(infile))

use a certain part of the string. A part before and after certain criteria in python

I have an array of full path files for example:
a=['/dir1/europe_2013_info.csv', '/dir2/USA_2013_info.csv', '/dir3/Africa_2013_info.csv']
I want to create a new file and write this characteristic into this file. for example:
f=open('all.csv','w')
f.write('port,europe\n')
f.close
f=open('all.csv','w')
f.write('port,USA\n')
f.close
etc.
I am fairly new in python. can someone help me how I can create a loop like this?
You don't need to open and close the file several times.
Use regular expression /(\w+)_\d{4} (or more explicit /(\w+)_\d{4}_info\.csv) to extract the required part of the file paths in the list and csv.writer to write to the csv file:
import re
import csv
a = ['/dir1/europe_2013_info.csv', '/dir2/USA_2013_info.csv', '/dir3/Africa_2013_info.csv']
regexp = '/(\w+)_\d{4}'
with open('all.csv', 'w') as f:
writer = csv.writer(f)
writer.writerows([['port', re.search(regexp, i).group(1)] for i in a])
then, the contents of all.csv will be:
port,europe
port,USA
port,Africa
Looks like
import os.path
b=[os.path.basename(x).split('_')[0] for x in a]
with open('all.csv.', 'w') as f:
for x in b:
f.write('port,{0}\n'.format(b))
may do something similar to what you need.

Write and read a list from file

This is a slightly weird request but I am looking for a way to write a list to file and then read it back some other time.
I have no way to remake the lists so that they are correctly formed/formatted as the example below shows.
My lists have data like the following:
test
data
here
this
is one
group :)
test
data
here
this
is another
group :)
If you don't need it to be human-readable/editable, the easiest solution is to just use pickle.
To write:
with open(the_filename, 'wb') as f:
pickle.dump(my_list, f)
To read:
with open(the_filename, 'rb') as f:
my_list = pickle.load(f)
If you do need them to be human-readable, we need more information.
If my_list is guaranteed to be a list of strings with no embedded newlines, just write them one per line:
with open(the_filename, 'w') as f:
for s in my_list:
f.write(s + '\n')
with open(the_filename, 'r') as f:
my_list = [line.rstrip('\n') for line in f]
If they're Unicode strings rather than byte strings, you'll want to encode them. (Or, worse, if they're byte strings, but not necessarily in the same encoding as your system default.)
If they might have newlines, or non-printable characters, etc., you can use escaping or quoting. Python has a variety of different kinds of escaping built into the stdlib.
Let's use unicode-escape here to solve both of the above problems at once:
with open(the_filename, 'w') as f:
for s in my_list:
f.write((s + u'\n').encode('unicode-escape'))
with open(the_filename, 'r') as f:
my_list = [line.decode('unicode-escape').rstrip(u'\n') for line in f]
You can also use the 3.x-style solution in 2.x, with either the codecs module or the io module:*
import io
with io.open(the_filename, 'w', encoding='unicode-escape') as f:
f.writelines(line + u'\n' for line in my_list)
with open(the_filename, 'r') as f:
my_list = [line.rstrip(u'\n') for line in f]
* TOOWTDI, so which is the one obvious way? It depends… For the short version: if you need to work with Python versions before 2.6, use codecs; if not, use io.
As long as your file has consistent formatting (i.e. line-breaks), this is easy with just basic file IO and string operations:
with open('my_file.txt', 'rU') as in_file:
data = in_file.read().split('\n')
That will store your data file as a list of items, one per line. To then put it into a file, you would do the opposite:
with open('new_file.txt', 'w') as out_file:
out_file.write('\n'.join(data)) # This will create a string with all of the items in data separated by new-line characters
Hopefully that fits what you're looking for.
Let's define a list first:
lst=[1,2,3]
You can directly write your list to a file:
f=open("filename.txt","w")
f.write(str(lst))
f.close()
To read your list from text file first you read the file and store in a variable:
f=open("filename.txt","r")
lst=f.read()
f.close()
The type of variable lst is of course string. You can convert this string into array using eval function.
lst=eval(lst)

Categories