Appending separate rows from csv in python - python

I want to append separate rows from a csv file. I want to append the first, third, fourth, and fifth element. Here is what I have so far but it doesn't seem to work — any ideas? This code currently saves into two separate lists; I want the rows to be appended into the same list.
import csv
game_file = open(gamefile + ".csv")
game_file = []
score_file.append([row[0]])
score_file.append([row[2:5]])
with open(studentsclass + ".csv", "w") as f:
writer = csv.writer(f)
writer.writerows(score_file)
f.close()
game_file.close()

Using csv could help:
import csv
game_file = []
included_cols = [0, 2, 3, 4, 5]
with open(filename, 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=';', quotechar='"')
for row in reader:
game_file.append(list(row[i] for i in included_cols))

You can use enumerate on your reader object and check if the index is in your desired list (which I have converted it to set that has O(1) for check membership ):
import csv
temp=0
rows=[]
with open('eggs.csv', 'rb') as csvfile:
spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
for i,row in enumerate(spamreader):
if i in {0,2,3,4,5}:
temp+=1
rows.append(row)
if temp==5:
print rows
break

Related

Adding/appending additional information in a column for a csv file

EDIT:
I need to store/add/append additional information in a specific column in a csv file with out using csv.DictReader.
If I wanted to skip a row in a column and it was empty, what do I need to do for it?
For example:
Sample csv file:
$ cat file.csv
"A","B","C","D","E"
"a1","b1","c1","d1","e1"
"a2","b2","c2","d2","e2"
"a2","b2","c2",,"e2"
Code:
sample = ['dx;dy']
with(openfile.csv, "r") as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
headers = next(reader)
for row in reader:
#sample.append(to the column D)
The Output should look like this:
$ cat file.csv
"A","B","C","D","E"
"a1","b1","c1","d1;dx;dy","e1"
"a2","b2","c2","d2;dx;dy","e2"
"a2","b2","c2",,"e2"
Since you know the header of the column you want to append to, you can find its index in the headers row, and then modify that element of each row.
append_to_column = 'D'
separator = ';'
sample = ['dx;dy']
with open('file.csv', "r") as csvfile, open("outfile.csv", "w") as outfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
headers = next(reader)
writer = csv.writer(outfile, delimiter=',', quotechar='"')
col_index = headers.index(append_to_column)
for row in reader:
value = row[col_index]
new_value = value + separator + sample[0]
row[col_index] = new_value
writer.writerow(row)
Which gives:
A,B,C,D,E
a1,b1,c1,d1;dx;dy,e1
a2,b2,c2,d2;dx;dy,e2
Note that this file doesn't have quotes because they aren't required, since the fields don't contain any commas. If you want to force the csv.writer to write quotes, you can add the quoting=csv.QUOTE_ALL argument to the csv.writer() call, like so: writer = csv.writer(outfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
Then, you'll get:
"A","B","C","D","E"
"a1","b1","c1","d1;dx;dy","e1"
"a2","b2","c2","d2;dx;dy","e2"

Python: replacing some rows of a csv but not others

I am trying to create a new csv file from an original. The new csv file should be a copy of the old, with the exception that a range of values in one column is multiplied by a constant. The values to alter occur from rows i to j inclusive. Here is the code I am attempting:
import csv
import itertools
i, j = 2, 10785
infile = open('../../combined_kW.csv', 'r')
outfile = open('../../combined_kW_adj.csv', 'w')
reader = csv.reader(infile, delimiter= ',')
datawriter = csv.writer(outfile, delimiter=',')
datawriter.writerow(['date', 'PVkW', 'TBLkW'])
next(reader) # there is a header row
for row in reader:
for row in itertools.islice(reader, i, j):
row[1] = row[1].replace(row[1], str(float(row[1]) * 5))
datawriter.writerow((row[0], row[1], row[2]))
From a csv with roughly 25,000 rows, the contents of the returned file are only:
date,PVkW,TBLkW
2016/04/04 03:00,0.0,207.23748999999998
2017/07/19 09:00,2921.5,287.15625
2018/01/12 18:00,0.0,267.9414
None of which are related to the rows i and j designated above. How can I better go about this?
import csv
i, j = 2, 10785
# assuming Python 3; otherwise omit 'newline'
with open('../../combined_kW.csv', 'r', newline='') as f:
r = csv.reader(f, delimiter=',')
rows = list(r)
# slice creates a shallow copy
# meaning each element still points to the same array element in rows!
for row in rows[i:j]:
row[1] = row[1].replace(row[1], str(float(row[1]) * 5))
with open('../../combined_kW_adj.csv', 'w', newline='') as f:
w = csv.writer(f, delimiter=',')
w.writerows(rows)

csv file to list in python

I have a CSV file which looks like this:
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_02,983,0,Prod,983
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_03,124,0,Prod ,124
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_04,206,0,Prod,206
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_05,983,0,Prod ,983
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_06,564,0,Prod,564
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_07,189,0,Prod ,189
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_08,168,0,Prod,168
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_09,570,0,Prod ,570
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_10,189,0,Prod,189
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_11,204,0,Prod ,204
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_12,189,2,Prod,187
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_13,568,0,Prod ,568
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_14,204,0,Prod,204
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_15,142,0,Prod ,142
File,2/13/2017,domain\test_roi,root_user,ntsrv1,/vol/vol_ntsrv1_16,168,0,Prod,168
I want to add to a list the 4th column (root_user) and the 7th column (where the numbers are written). Any suggestions how?
import csv
four_col, seven_col = [], []
with open(file='test.csv', mode='r', encoding='utf-8') as csvfile:
spamreader = csv.reader(csvfile, delimiter=',', quotechar='"')
# firstline = csvfile.readline() # if csv have header uncomment it
for row in spamreader:
four_col.append(row[3])
seven_col.append(row[6])
With this csv file you can read it also setting the spamreader as:
spamreader = csv.reader(csvfile, dialect='excel')
but I wrote you the more generic way if the file don't uses commas for delimiter.
It's pretty simple this way:
fourth_column_list = []
seventh_column_list = []
with open(my_csv_file, 'r') as infile:
parsed = (x.split(',') for line in infile) # get all parsed columns
for parsed_line in parsed: # iterate over parsed lines
fourth_column_list.append(parsed_line[3]) # append 4th column
seventhth_column_list.append(parsed_line[6]) # append 7th column

Printing out csv rows apart from first row

I want to read a CSV file in Python, and then print out every row apart from the first row.
I know how to print out all the rows:
with open('myfile.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
print row
And the only way I can think of not printing out the first row is:
with open('myfile.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for i, row in enumerate(reader):
if i != 0:
print row
But this doesn't seem very elegant. Any other solutions?
csv reader objects are iterators, which means you can skip single entries using next():
with open('myfile.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
next(reader) # just ignore the result
for row in reader:
print row

How to read one single line of csv data in Python?

There is a lot of examples of reading csv data using python, like this one:
import csv
with open('some.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row)
I only want to read one line of data and enter it into various variables. How do I do that? I've looked everywhere for a working example.
My code only retrieves the value for i, and none of the other values
reader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in reader:
i = int(row[0])
a1 = int(row[1])
b1 = int(row[2])
c1 = int(row[2])
x1 = int(row[2])
y1 = int(row[2])
z1 = int(row[2])
To read only the first row of the csv file use next() on the reader object.
with open('some.csv', newline='') as f:
reader = csv.reader(f)
row1 = next(reader) # gets the first line
# now do something here
# if first row is the header, then you can do one more next() to get the next row:
# row2 = next(f)
or :
with open('some.csv', newline='') as f:
reader = csv.reader(f)
for row in reader:
# do something here with `row`
break
you could get just the first row like:
with open('some.csv', newline='') as f:
csv_reader = csv.reader(f)
csv_headings = next(csv_reader)
first_line = next(csv_reader)
You can use Pandas library to read the first few lines from the huge dataset.
import pandas as pd
data = pd.read_csv("names.csv", nrows=1)
You can mention the number of lines to be read in the nrows parameter.
Just for reference, a for loop can be used after getting the first row to get the rest of the file:
with open('file.csv', newline='') as f:
reader = csv.reader(f)
row1 = next(reader) # gets the first line
for row in reader:
print(row) # prints rows 2 and onward
From the Python documentation:
And while the module doesn’t directly support parsing strings, it can easily be done:
import csv
for row in csv.reader(['one,two,three']):
print row
Just drop your string data into a singleton list.
The simple way to get any row in csv file
import csv
csvfile = open('some.csv','rb')
csvFileArray = []
for row in csv.reader(csvfile, delimiter = '.'):
csvFileArray.append(row)
print(csvFileArray[0])
To print a range of line, in this case from line 4 to 7
import csv
with open('california_housing_test.csv') as csv_file:
data = csv.reader(csv_file)
for row in list(data)[4:7]:
print(row)
I think the simplest way is the best way, and in this case (and in most others) is one without using external libraries (pandas) or modules (csv). So, here is the simple answer.
""" no need to give any mode, keep it simple """
with open('some.csv') as f:
""" store in a variable to be used later """
my_line = f.nextline()
""" do what you like with 'my_line' now """

Categories