Right now I have several long lists : One called variable_names.
Lets say Variable names= [ Velocity, Density, Pressure, ....] (length is 50+)
I want to write a row that reads every index of the list, leaves about 5 empty cells, then writes next value, and keeps doing it until list is done.
As shown in row1 Sample picture
The thing is I can't use xlrd due to compatibility issues with Iron Python and I need to dynamically write each row in the new csv , load data from old csv , then append that data in the new csv, the old csv keeps changing once I append the data in the new csv, so I need to iterate all values in the lists for every time I write the row, because it is much more difficult to append columns in csv.
What I basicall want to do is :
with open('data.csv','a') as f:
sWriter=csv.writer(f)
sWriter.writerow([Value_list[i],Value_list[i+1],Value_list[i+2].....Value_list[end])
But I can't seem to think of a way to do this with iteration
Because writerow method takes a list argument, you can first construct the list and then write the list so everything in the list will be in one row.
Like,
with open('data.csv','a') as f:
sWriter=csv.writer(f)
listOfColumns = []
for i in range(from, to): # append elements from Value_list
listOfColumns.append(Value_list[i])
for i in range(0, 2): # Or you may want some columns with blank
listOfColumns.append("")
for i in range(anotherFrom, anotherTo): # append elements from Value_list
listOfColumns.append(Value_list[i])
# At here, the listOfColumns will be [Value_list[from], ..., Value_list[to], "", "", Value_list[anotherFrom], ..., Value_list[anotherTo]]
sWriter.writerow(listOfColumns)
Related
I am using openpyxl to work with excel on python.
I have a list i want to add each value inside it in excel file, my current code:
for y in myzoo:
loo1 = str(y)
c5a = my_sheet.cell(row= 21 , column = 3)
c5a.value = loo1
myzoo is the list (its originally a pyodbc.Row)
and i convert each entry to string, then save it to excel file, the problem is currently it save only last one overwriting all earlier values, i want to do one of two: save each value in next empty cell in row, or even (which less preferable) saving all the exported data into the cell without deleting earlier ones, thanks.
I think you can just do something like this:
column = 3 # start column
while myzoo:
c5a = my_sheet.cell(row=21, column=column)
if not c5a.value:
c5a.value = str(myzoo.pop(0))
column += 1
in case you need to preserve myzoo - you will need to copy it. (temp = myzoo.copy())
I am trying to make a leader board for my python program
I have sorted out writing the different scores to the leader board already
However, I am having trouble finding a way that I can sort this data
(Highest score at the top and lowest at the bottom)
Also, I am sorry but I do not have any code that is even vaguely functional, everything I have tried has just been incorrect
Also I only have limited access to modules as it is for a school project which makes it even harder for me (I have CSV,Random,Time,)
Thank you so much
I would really appreciate any help I can recieve
You can read in the file with pandas, sort it by a column, and overwrite the old csv with the new values. The code would look similar to this:
import pandas as pd
path = your_file_path
df = pd.read_csv(path)
df = df.sort_values(by=["column_name"], ascending=False)
df.to_csv(path)
This problem can be done in 3 parts using standard Python:
Read all of the data (assuming it has a header row). A csv_reader() is used to parse your file and read in each row as a list of values. By calling list() it will read all rows as a list of rows.
Sort the data
Write all of the data (add back the header first), this time using a csv.writer() to automatically take your list of rows and write the correct format to the file.
This can be done using Python's csv library which you say you can use. Secondly you need to tell the sort() function how to sort your rows. In this example it assumes the scores are in the second column. The csv library will read each row as a list of values (starting from 0), so the score in this example is column 1. The key parameter gives sort() a function to call for each row that it is sorting. The function receives a row and returns which parts of the row to sort on, that way you don't have to sort on the first column. lambda is just shorthand for writing a single line function, it takes a parameter x and returns the elements from the row to sort on. Here we use a Python tuple to return two elements, the score and the name. First convert the score string x[1] into an integer. Adding a - will make the highest score sort to the top. x[0] then uses the Name column to sort for cases where two scores are the same:
import csv
with open('scores.csv', newline='') as f_input:
csv_input = csv.reader(f_input)
header = next(csv_input)
data = list(csv_input)
data.sort(key=lambda x: (-int(x[1]), x[0]))
with open('scores_sorted.csv', 'w', newline='') as f_output:
csv_output = csv.writer(f_output)
csv_output.writerow(header)
csv_output.writerows(data)
So for a sample CSV file containing:
name,score
fred,5
wilma,10
barney,8
betty,4
dino,10
You would get a sorted output CSV looking like:
name,score
dino,10
wilma,10
barney,8
fred,5
betty,4
Note, dino and wilma both have the same score, but dino is alphabetically first.
This assumes you are using Python 3.x
Update. Here is my code. I am importing 400 csv files into 1 list. Each csv file is 200 rows and 5 columns. My end goal is to sum the values from the 4th column of each row or each csv file. The below code imports all the csv files. However, I am struggling to isolate 4th column of data from each csv file from the large list.
for i in range (1, 5, 1):
data = list()
for i in range(1,400,1):
datafile = 'particle_path_%d' % i
data.append(np.genfromtxt(datafile, delimiter = "", skip_header=2))
print datafile
I want to read 100 csv files into 100 different arrays in python. For example:
array1 will have csv1
array2 will have csv2 etc etc.
Whats the best way of doing this? I am appending to a list right now but I have one big list which is proving difficult to split into smaller lists. My ultimate goal is to be able to perform different operations of each array (add, subtract numbers etc)
Could you provide more detail on what needs to be done? If you are simply trying to read line by line in the csv files and make that the array then this should work:
I would create a 2 dimensional array for this, something like:
csv_array_container = []
for csv_file in csv_files:
csv_lines = csv_file.readlines()
csv_array_container.append(csv_lines)
#Now close your file handlers
Assuming that csv_files is a list of open file_handlers for the csv files. Something more appropriate would likely open the files in the loop and close them after use rather than open 100, gather data, and close 100 due to limits on file handlers.
If you would like more detail on this, please give us more info on what you are exactly trying to do with examples. Hope this helps.
So you have a list of 100 arrays. What can you tell us about their shapes?
If they all have the same shape you could use
arr = np.stack(data)
I expect arr.shape will be (100,200,5)
fthcol = arr[:,:,3] # 4th column
If they aren't all the same, then a simple list comprehension will work
fthcol = [a[:,3] for a in data]
Again, depending on the shapes you could np.stack(fthcol) (choose your axis).
Don't be afraid to iterate over the elements of the data list. With 100 items the cost won't be prohibitive.
I am going to start off with stating I am very much new at working in Python. I have a very rudimentary knowledge of SQL but this is my first go 'round with Python. I have a csv file of customer related data and I need to output the records of customers who have spent more than $1000. I was also given this starter code:
import csv
import re
data = []
with open('customerData.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
data.append(row)
print(data[0])
print(data[1]["Name"])
print(data[2]["Spent Past 30 Days"])
I am not looking for anyone to give me the answer, but maybe nudge me in the right direction. I know that it has opened the file to read and created a list (data) and is ready to output the values of the first and second row. I am stuck trying to figure out how to call out the column value without limiting it to a specific row number. Do I need to make another list for columns? Do I need to create a loop to output each record that meets the > 1000 criteria? Any advice would be much appreciated.
To get a particular column you could use a for loop. I'm not sure exactly what you're wanting to do with it, but this might be a good place to start.
for i in range(0,len(data)):
print data[i]['Name']
len(data) should equal the number of rows, thus iterating through the entire column
The sample code does not give away the secret of data structure. It looks like maybe a list of dicts. Which does not make much sense, so I'll guess how data is organized. Assuming data is a list of lists you can get at a column with a list comprehension:
data = [['Name','Spent Past 30 Days'],['Ernie',890],['Bert',1200]]
spent_column = [row[1] for row in data]
print(spent_column) # prints: ['Spent Past 30 Days', 890, 1200]
But you will probably want to know who is a big spender so maybe you should return the names:
data = [['Name','Spent Past 30 Days'],['Ernie',890],['Bert',1200]]
spent_names = [row[0] for row in data[1:] if int(row[1])>1000]
print(spent_names) # prints: ['Bert']
If the examples are unclear I suggest you read up on list comprehensions; they are awesome :)
You can do all of the above with regular for-loops as well.
I am trying to read a list of values in a column in an excel sheet. However, the length of the column varies every time, so I don't know the length of the list. How do I get python to read all the values in a column and stop when the cells are empty using xlrd?
for i in range(worksheet.nrows):
will iterate through all the rows in the worksheet
if you were interested in column 0 for example
c0 = [worksheet.row_values(i)[0] for i in range(worksheet.nrows) if worksheet.row_values(i)[0]]
or even better make this a generator
column_generator = (worksheet.row_values(i)[0] for i in range(worksheet.nrows))
then you can use itertools.takewhile for lazy evaluations... that will stop when you get your first empty... this will provide better performance if you just want to stop once you get your first empty value
from itertools import takewhile
print list(takewhile(str.strip,column_generator))