How to write a table line by line with for loop - python

I have different files in where I pick up some data/values in order to produce a table grouping everything.
Here is a small example of the code I am using:
stations = ["AAA", "BBB", "CCCC", "DDDD"]
datadir = "/home/data/"
table = []
for station in stations:
os.chdir(datadir)
nc = Dataset(station + ".nc", 'r+')
p = (nc.variables['Rainf'][:,0,0]
evap = nc.variables['Qle'][:,0,0]
table.append(p)
table.append(evap)
table_t=list(table)
with open (datadir + "table.csv", 'w') as ofile:
writer = csv.writer(ofile)
writer.writerow(table_t)
But this code only write all the results of all stations in one row. What do I need to change in order that for each station the code write the data/values on the next row?

You want to use writer.writerows(table_t) instead.
writerows() method takes the iteration and creates lines per item in the list.
Example:
data = [list(i) for i in 'abcde fhghi jklmn opqrs'.split()]
# [['a', 'b', 'c', 'd', 'e'],
# ['f', 'h', 'g', 'h', 'i'],
# ['j', 'k', 'l', 'm', 'n'],
# ['o', 'p', 'q', 'r', 's']]
with open('test.csv','w') as file:
writer = csv.writer(file, lineterminator='\n')
writer.writerows(data)
# test.csv
# a,b,c,d,e
# f,h,g,h,i
# j,k,l,m,n
# o,p,q,r,s

you need to iterate over the table you want to write out:
with open (datadir + "table.csv", 'w') as ofile:
writer = csv.writer(ofile)
for row in table:
writer.writerow(row)
hope that helps.

Related

Convert list of strings into dictionary

I would like to convert a list of strings into a dictionary.
The list looks like such after I have split it into the seperate words:
[['ice'], ['tea'], ['silver'], ['gold']]
Which I want to convert to a dictionary which looks like such:
{ 1 : ['i', 'c', 'e']
2 : ['t','e','a']
3 : ['s','i','l','v','e','r']
4 : ['g','o','l','d']}
This is my code thus far:
import itertools
def anagram1(dict):
with open('words.txt', 'r') as f:
data = f.read()
data = data.split()
x = []
y = []
for word in data:
x1 = word.split()
x.append(x1)
for letters in word:
y1 = letters.split()
y.append(y1)
d = dict(itertools.zip_longest(*[iter(y)] * 2, fillvalue=""))
To which I receive the following error:
TypeError: 'dict' object is not callable
import pprint
l = [['ice'], ['tea'], ['silver'], ['gold']]
d = {idx: list(item[0]) for idx, item in enumerate(l, start =1)}
pprint.pprint(d)
{1: ['i', 'c', 'e'],
2: ['t', 'e', 'a'],
3: ['s', 'i', 'l', 'v', 'e', 'r'],
4: ['g', 'o', 'l', 'd']}
Following should do the job:
with open('file.txt', 'r') as f:
data = f.read()
data = data.split()
data_dict = {i:v for i,v in enumerate(data)}

Add column number to every column

Im reading txt file and add array row by row. but I need to change every row like this
My list like = [[1strow],[2ndrow],[3rdrow],........,[8000throw]]. ıts like list in list.
My rows : Every row contain 23 letters but I only want to change 2-23 not first one.
e,a,b,c,d,r,y,t,w,s,e,t......s (23th letter , but If you start 0 cause of index, Its 22th)
t,y,e,e,s,f,g,r,t,q,w,e,r,.....s
What I want is
e,a1,b2,c3,d4,r5,y6,t7,w8,s9,e10,t11......s22
t,y1,e2,e3,s4,f5,g6,r7,t8,q9,w10,e11,r12,.....a22
My main code :
with open('C:/Users/xxx/Desktop/input/mushrooms.csv', 'r') as csvfile:
spamreader = csv.reader(csvfile)
for row in spamreader:
datas.append(row)
print(datas[0]) --> ['p', 'x', 's', 'n', 't', 'p', 'f', 'c', 'n', 'k', 'e', 'e', 's', 's', 'w', 'w', 'p', 'w', 'o', 'p', 'k', 's', 'u']
How can I do that with python ?
row = ['e','a','b','c','d','r','y','t','w','s','e','t']
newrow = row[0:1] + [letter + str(num) for num,letter in enumerate(row[1:],1)]
In your specific example,
newdatas = [row[0:1] + [letter + str(num) for num,letter in enumerate(row[1:],1)] for row in datas]

Python loops are missing results

I am reading a file with about 13,000 names on it into a list.
Then, I look at each character of each item on that list and if there is a match I remove that line from the list of 13,000.
If I run it once, it removes about half of the list. On the 11th run it seems to cut it down to 9%. Why is this script missing results? Why does it catch them with successive runs?
Using Python 3.
with open(fname) as f:
lines = f.read().splitlines()
bad_letters = ['B', 'C', 'F', 'G', 'H', 'J', 'L', 'O', 'P', 'Q', 'U', 'W', 'X']
def clean(callsigns, bad):
removeline = 0
for line in callsigns:
for character in line:
if character in bad:
removeline = 1
if removeline == 1:
lines.remove(line)
removeline = 0
return callsigns
for x in range (0, 11):
lines = clean(lines, bad_letters)
print (len(lines))
You are changing (i.e., mutating) the lines array while you're looping (i.e. iterating) over it. This is never a good idea because it means that you are changing something while you're reading it, which leads to you skipping over lines and not removing them in the first go.
There are many ways of fixing this. In the below example, we keep track of which lines to remove, and remove them in a separate loop in a way so that the indices do not change.
with open(fname) as f:
lines = f.read().splitlines()
bad_letters = ['B', 'C', 'F', 'G', 'H', 'J', 'L', 'O', 'P', 'Q', 'U', 'W', 'X']
def clean(callsigns, bad):
removeline = 0
to_remove = []
for line_i, line in enumerate(callsigns):
for b in bad:
if b in line:
# We're removing this line, take note of it.
to_remove.append(line_i)
break
# Remove the lines in a second step. Reverse it so the indices don't change.
for r in reversed(to_remove):
del callsigns[r]
return callsigns
for x in range (0, 11):
lines = clean(lines, bad_letters)
Save the names you want to keep in a separate list.. Maybe this way:-
with open(fname) as f:
lines = f.read().splitlines()
bad_letters = ['B', 'C', 'F', 'G', 'H', 'J', 'L', 'O', 'P', 'Q', 'U', 'W', 'X']
def clean(callsigns, bad):
valid = [i for i in callsigns if not any(j in i for j in bad)]
return valid
valid_names = clean(lines,bad_letters)
print (len(valid_names))

Loading an unknown amount of pickled objects in python

I have a small and simple movie registration app that lets a user register a new movie in the registry. This is currently only using pickled objects and saving the objects is not a problem but reading an unknown number of pickled objects from the file seems to be a little more complicated since i cant find any sequence of objects to iterate over when reading the file.
Is there any way to read an unknown number of pickled objects from a file in python (read into an unknown number of variables, preferably a list) ?
Since the volume of the data is so low i dont see the need to use a more fancy storage solution than a simple file.
When trying to use a list with this code:
film = Film(title, description, length)
film_list.append(film)
open_file = open(file, "ab")
try:
save_movies = pickle.dump(film_list, open_file)
except pickle.PickleError:
print "Error: Could not save film to file."
it works fine and when i load it i get a list returned but no matter how many movies im registering i still only get one element in the list. When typing len(film_list) it only returns the first movie that was saved/added to the file. When looking at the file it does contain the other movies that were added to the list but they are not being included in the list for some strange reason.
I'm using this code for loading the movies:
open_file = open(file, "rb")
try:
film_list = pickle.load(open_file)
print type(film_list) # displays a type of list
print len(film_list) # displays that only 1 element is in the list
for film in film_list: # only prints out one list item
print film.name
except pickle.PickleError:
print "Error: Unable to load one or more movies."
You can get an unknown amount of pickled objects from a file by repeatedly calling load on a file handle object.
>>> import string
>>> # make a sequence of stuff to pickle
>>> stuff = string.ascii_letters
>>> # iterate over the sequence, pickling one object at a time
>>> import pickle
>>> with open('foo.pkl', 'wb') as f:
... for thing in stuff:
... pickle.dump(thing, f)
...
>>>
>>> things = []
>>> f = open('foo.pkl', 'rb')
>>> # load the first two objects
>>> things.append(pickle.load(f))
>>> things.append(pickle.load(f))
>>> # get the remaining pickled items
>>> while True:
... try:
... things.append(pickle.load(f))
... except EOFError:
... break
...
>>> stuff
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> things
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
>>> f.close()

how do you split elements of a list of list in python?

So I have a text file that looks like this:
abcd
efghij
klm
and I need to convert it into a two-dimensional list. And it should look like this:
[['a', 'b', 'c', 'd'],
['e', 'f', 'g', 'h', 'i', 'j'],
['k', 'l', 'm']]
so far I have managed to get this result:
[["abcd"], ["efghij"], ["klm"]]
Can anyone help me figure out what the next step should be?
Here is my code so far:
def readMaze(filename):
with open(filename) as textfile:
global mazeList
mazeList = [line.split() for line in textfile]
print mazeList
str.split() splits on whitespace. str.split('') splits each character separately. (apparently I'm misremembering, str.split('') throws a ValueError for "empty separator")
You'll just build a list from it.
text = """abcd
efghij
klm"""
mazelist = [list(line) for line in text.splitlines()]
# the splitlines call just makes it work since it's a string not a file
print(mazelist)
# [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h', 'i', 'j'], ['k', 'l', 'm']]
Make a list of each line in the file:
with open('tmp.txt') as f:
z = [list(thing.strip()) for thing in f]
As explained above, you just need to build a list from the strings.
Assuming the string is held in some_text;
lines = some_text.split('\n')
my_list = []
for line in lines:
line_split = list(line)
my_list.append(line_split)
as one-liner;
my_list = map(lambda item: list(item), some_text.split('\n'))
should do the trick.

Categories

Resources