I'm using Python, and with the library gdata I can upload a .csv file, but the delimiter stays as default, that is comma (","). How can I do to change the delimiter to, for example, ";" ?
What I want is, from Python, change the delimiter of an uploading file. I don't want to change the "," to ";", I want to change the delimiter.
You could open the .csv using excel, then it knows its a csv (Comma Delimeted file) but you can set other things to 'delimit' the file by such as spaces etc.
Edit: Sorry, should've mentioned, don't open the file using the 'open with' method, open Excel first, then open the file from within Excel. This should open the 'Text Import Wizard' - Where you can choose what to delimt the file with such as tab,semicolon,comma,space etc.
I am assuming you really need to select this delimiter through gdata, right?
Otherwise you can easily change the delimiter in a shell with something like:
cat my_csv.csv | tr ',' ';' > my_csv_other_delimiter.csv
You can also easily replace these symbols in your python code. It could be an overload if you receive your csv files from somewhere else and you cannot control the symbol you use as a delimiter, but if there is no choice that could be an option.
Related
This is the file set up:
text_file.csv
Folder----code.py
What I want do do is have code.py write "bob" in text_file.csv.
(Note: a .csv file is just a text file.)
NOTE: csv file is not equal as text file, csv is a comma-separated-values file, and it is formatted in a different way, you should watch out for these things, for more informations, visit the wikipedia page
if you want to write the content you can use the built-in open function:
open(Path,mode).write(content)
if you want to overwrite the content use 'w' mode:
open("..\\text_file.csv","w").write("bob")
if you want to add it append it, you can use the 'a' mode:
open("..\\text_file.csv",'a').write("bob")
but if you want to add it as a newline, you should add a '\n' in fromt of the "bob"(the text you want to put in):
open("..\\text_file.csv","a").write('\n'+"bob") #adding a newline to 'bob'
I use python to create a csv which is UTF-8. it use ',' as delimiter
then, i use excel to open the file even i press save it.
I use notepad++ open it again
It change delimiter ',' to \t [tab]
is that any excel setting problem?
So I have this code that generates a .csv file of data, however the formatting is off due to the escapechar (can't fix this). I need to make all the double spaces into single spaces. I can do this in notepad++ with replace all, so I've written a python script using a notepad++ plugin that does this. Now I'd like to automate opening the file and running the script; is this possible using a batch file? Is there a better way to do this?
Example of before and after format needed:
"_time","location"
"2018-04-03T08:32:45.565000-0400","(0 , 3)"
"2018-04-03T08:32:45.565000-0400","(2 , 5)"
"_time","location"
"2018-04-03T08:32:45.565000-0400","(0,3)"
"2018-04-03T08:32:45.565000-0400","(2,5)"
You can do it all with Python.
Just read the file and use the string replace method. Probably you will create a temporary file with the adjustments and then rename it. Something like:
with open(fname) as f:
lines = f.readlines()
for line in lines:
newline = line.replace(" ", " ") #two spaces become one space
#... write newline to temp file, etc.
Is it possible to specify the delimiting character the csv module will use when writing a file?
For example, instead of ',' use ';' or something?
I know you can change to tab delimited by setting dialect='excel-tab, but not sure if there is an option for freely choosing the delimiter.
Thanks
I believe you can just set the delimiter:
writer = csv.writer(csvfile,delimiter=';')
There's also an example of this in the documentation for csv.writer
I have a csv file which contains rows from a sqlite3 database. I wrote the rows to the csv file using python.
When I open the csv file with Ms Excel, a blank row appears below every row, but the file on notepad is fine(without any blanks).
Does anyone know why this is happenning and how I can fix it?
Edit: I used the strip() function for all the attributes before writing a row.
Thanks.
You're using open('file.csv', 'w')--try open('file.csv', 'wb').
The Python csv module requires output files be opened in binary mode.
the first that comes into my mind (just an idea) is that you might have used "\r\n" as row delimiter (which is shown as one linebrak in notepad) but excel expects to get only "\n" or only "\r" and so it interprets this as two line-breaks.