python


Saving a csv with excel so python can append rows correctly


When ever I save a csv file from excel (Mac version 15.17) appending new rows with python doesn't work as desired
e.g.I save this file in excel:
col1 col2 col3
-----------------
1234 1234 1234
1234 1234 1234
and I append these lines with python
5678 5678 5678
5678 5678 5678
I get:
col1 col2 col3
1234 1234 1234
1234 1234 12345678 5678 5678
5678 5678 5678
As if excel misses off the last line break. Is there a way to get around this without manually adding (with code) a line break at the end of every excel file.
Opening the file with a text editor and adding a line break fixes the problem but that's a horrible work around.
Code used in python to append a line to the csv:
with file(append_to_this_csv.csv, 'a') as writefile:
writer = csv.writer(writefile)
row = [[5678,5678,5678]]
writer.writerows(row)
Try adding in the trailing newlines in your Python script instead of using a text editor:
def fix_trailing_newline(fname):
with open(fname, "r+") as f:
f.seek(-1, 2)
if(f.read() != '\n'):
f.seek(0, 2)
f.write('\n')
Alternatively, you can change how you append rows to a csv file. You could read in the entire file, append your rows in memory, and then write the entire file:
def append(fname, data):
with open(fname) as f:
reader = csv.reader(f)
data = list(reader) + list(data)
with open(fname, 'w') as f:
writer = csv.writer(f)
writer.writerows(data)

Related Links

How to do a Python argparse mutually required argument group
scaling websocket game application server
Is there a way to find a character's Unicode code point in Python 2.7?
figtext datetime function matplotlib
environment variable in ubuntu
Cross-platform Python Executables
Extract subarray from collection of 2D coordinates?
tkinter populate treeview using threading pool
How to make a function determining the winner of Tic-Tac-Toe more concise
Django update model entry using form fails
ctypes using HRESULT(python)
How to export property values with django-import-export
Plotting Coordinate Lines Using Matplotlib
BASH - Summarising information present in 2 genotype data columns in one column (ped file) [closed]
Python 3: How to call function from another file and pass arguments to that function ?
Streaming mp3 files in Django through Nginx

Categories

HOME
symfony
app-inventor
payment-gateway
meshlab
warnings
textwatcher
visual-studio-2013
facebook-graph-api
thunderbird-addon
codeeffects
is-empty
diagram
getorgchart
google-shopping
simple-injector
symfony2-easyadmin
r-lavaan
aspell
bootstrap-popover
google-people
attask
dspic
go-cd
npm-install
searchbar
compare-and-swap
java-ee-7
pipelinedb
hash-collision
i3
vertex-buffer
websauna
protobuf-net
hotmail
geo
geomesa
pox
rider
zero
common.logging
homekit
jdbi
encapsulation
openweathermap
ogre3d
forerunnerdb
jexl
instruction-set
butterknife
quintus
iphone-developer-program
jcreator
date-range
shinyjs
build-process
ruby-2.0
dts
spark-cassandra-connector
unsatisfiedlinkerror
kendo-combobox
faraday
concur
qt-linguist
handlebars.java
altbeacon
servlet-3.0
android-cursoradapter
data-import
intellitest
achartengine
digits
debian-based
ticket-system
jai
ejabberd-saas
jcr-sql2
libsndfile
dmp
ghostdoc
docopt
dalekjs
funq
planetary.js
korma
dbsetup
magicalrecord-2.2
confusion-matrix
asp.net-mvc-scaffolding
trimming
installshield-2009
ng-hide
process-monitor
crocodoc
osi
couchdb-lucene
xgettext
n-layer
symfony-2.0
userid
cross-domain-policy
newsstand-kit
testunit
smooth
nagle
squeel
chdatastructures
winverifytrust
microsoft.ink
kpi
opcodes
virtualquery
handheld
castle-validators
project-lifecycle
rd

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App