Forums

How to manipulate csv files

Hi Experts,

I am new to Python, i have 1000+ rows in my csv file there am having Data for the different Dates, each Dates Contains more than 100 values, now i want to count the no of rows having the same date and want to create a new column for this output.

is there any way to do this manipulation..?

if yes, pls help me to solve this issue.!! its very urgent.

Thanks & Regards, Prakruthi

Yes, there is. Have a look at the Python CSV module. That's very useful for reading CSV files.

Hi Experts,

I want to insert a serial number(row number) in to my csv file, but my csv file have headers so i need to insert serial number from second line. Here is my code..

f_in=open("testingcsv.csv","r") f_out=open("testing123.csv","w") i=1 for line in f_in: print(line.rstrip()) f_out.write(str(i)+" "+line) i=i+1 f_in.close() f_out.close()

Please help me to solve this issue!!! its urgent Thanks in advance :-)

just make a call to f_in.readline() before starting the for loop.

Hi Experts, I have csv file, in that some rows contains ",""(comma and double quotes) at the end of some rows. So i have to delete those particular characters (,").

I tried the following

import pandas as pd
tr=pd.read_csv('D:/python/testingcsv.csv')
del tr['col1']
print(tr)

But i the above code whole column is deleting, but i need to delete only the value which contain (,") character.

Please help me out to solve this issue..!

[edited by admin: formatting]

Are you getting a bunch of empty string or None's when there is an extra comma? You could use filter to filter out them.

If you are still having trouble, I would suggesting going to codementor for these sort of questions. You may find it super beneficial to get someone to walk you through it.

Hi Experts, i have multiple csv files in multiple folders, so i need to access whole csv files at once. I am bit confused to set the path for that csv files.. plz suggest me how to access thous csv files under the directories. Here is my code for csv manipulation, but i want to apply this effect for all csv file which is in that folder. Please help me out to solve this issue!!! Thanks in advance :-)

import pandas as pd
import csv.glob
***file_name=r"/home/Backup1/new/2015-11-26/view_data/v_board9/*.csv"***
def updated_csvfile():
    fo=open(file_name,encoding='utf-8')
    fread=fo.read()
    frep=fread.replace('\0','NULL')#nul byte is available in the csv file
    fo1=open(file_name,"w",encoding='utf-8')
    fo1.write(frep)
    fo.close()   
    fo1.close() 
def create_quotedcsvfile():
    d=pd.read_csv(file_name,header=None)
    d.to_csv(file_name, header=False,quoting=csv.QUOTE_ALL)
    d.to_csv(file_name, header=False,quoting=csv.QUOTE_ALL)
updated_csvfile()
create_quotedcsvfile()

[edited by admin for formatting]

How about something like this:

import os
folder = "/home/Backup1/new/2015-11-26/view_data/v_board9/"
for filename in os.listdir(folder):
    full_path = os.path.join(folder, filename)
    updated_csvfile(full_path)
....

And then adjust your two functions so they take the file path as an argument?

If you need more help with Python, you can check out codementor