Your code is blanking out your file:
import csv
workingdir = "C:\Mer\Ven\sample"
csvfile = workingdir+"\test3.csv"
f=open(csvfile,'wb') # opens file for writing (erases contents)
csv.writer(f, delimiter =' ',quotechar =',',quoting=csv.QUOTE_MINIMAL)
if you want to read the file in, you will need to use csv.reader and open the file for reading.
import csv
workingdir = "C:\Mer\Ven\sample"
csvfile = workingdir+"\test3.csv"
f=open(csvfile,'rb') # opens file for reading
reader = csv.reader(f)
for line in reader:
print line
If you want to write that back out to a new file with different delimiters, you can create a new file and specify those delimiters and write out each line (instead of printing the tuple).
Answer from underrun on Stack OverflowYour code is blanking out your file:
import csv
workingdir = "C:\Mer\Ven\sample"
csvfile = workingdir+"\test3.csv"
f=open(csvfile,'wb') # opens file for writing (erases contents)
csv.writer(f, delimiter =' ',quotechar =',',quoting=csv.QUOTE_MINIMAL)
if you want to read the file in, you will need to use csv.reader and open the file for reading.
import csv
workingdir = "C:\Mer\Ven\sample"
csvfile = workingdir+"\test3.csv"
f=open(csvfile,'rb') # opens file for reading
reader = csv.reader(f)
for line in reader:
print line
If you want to write that back out to a new file with different delimiters, you can create a new file and specify those delimiters and write out each line (instead of printing the tuple).
ok, here is what i understood from your question. You are writing a csv file from python but when you are opening that file into some other application like excel or open office they are showing the complete row in one cell rather than each word in individual cell. I am right??
if i am then please try this,
import csv
with open(r"C:\\test.csv", "wb") as csv_file:
writer = csv.writer(csv_file, delimiter =",",quoting=csv.QUOTE_MINIMAL)
writer.writerow(["a","b"])
you have to set the delimiter = ","
I need to find out what separator is used in a csv file
According to the documentation of read_csv, you can use the Python engine to auto-detect the separator. Something like this should do:
dataframe = pandas.read_csv("filename.csv", sep=None, engine="python")This will make use of csv.Sniffer that can detect the separator and whether there's a header.
More on reddit.comMade a csv parser library which automatically detects delimiter and start and end of data
Hey guys!
I have been using pandas to read csv files but there is a problem. The person that provides me the files does not use the comma separator. In order to automate the process, I would like to find out what separator is used in the file. This is essential because I am storing the values from the csv in a database with a psycopg2 function called copy_expert and if the number of columns in the file do not match the number of columns in the database table, an error occurs saying that it expected a different number of fields.
Anyway, does anyone know how to find which separator is used in a csv file?
According to the documentation of read_csv, you can use the Python engine to auto-detect the separator. Something like this should do:
dataframe = pandas.read_csv("filename.csv", sep=None, engine="python")
This will make use of csv.Sniffer that can detect the separator and whether there's a header.
Ask the person which delimiter they have decided to use?
Decide between you a delimiter to use as standard going forward.
For most purposes, just passing the file path will be sufficient and you will have a pandas data frame of you data. Automatically neglects the random extra information at the top and bottom of a file. Also works with those annoying delimiter within quotes thingies (ex: you have a comma delimiter and one field is “John, Andrew and Steven”). It is on PyPI so you can download it with pip.
https://github.com/Oddball777/csv-scavenger
Teaching myself Python. Have hit a small roadblock. Below is my code.
import csv def save(): lst=[('a','b'),('c','d'),('e','f')] with open('something.csv','w', newline='') as csvfile: writer = csv.writer(csvfile, delimiter=',') for something in lst: writer.writerow(something) if name == 'main': save()
something.csv is: a,b c,d e,f
Is there a way to add a space between the comma and the next character. So that it looks something like this?:
a, b c, d e, f
Thank you for your help.