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

Is this essential functional programming feature missing from python?
Hooking into a wave-out on different platforms
What causes subprocess.call to output blank file when attempting db export with mysqldump?
How do I remove something form a list, plus string matching?
Reference to Part of List - Python
Django Delete all but last five of queryset
How do I limit the number of active threads in python?
Calculating the pixel size of a string with Python
Python nested lists and recursion problem
Problems PUTting binary data to Django
How to use long integers in Python to build a range?
Should I use Lex or a home-brewed solution to parse a formula?
Launching default application for given type of file, OS X
Python CGI script IOError Broken Pipe
Django, grouping query items
Regular expression works normally, but fails when placed in an XML schema

Categories

HOME
jdbc
jenkins-plugins
crate
warnings
label
ruby-on-rails-3
apple-numbers
sudo
jframe
onenote-api
diagram
hashicorp-vault
tee
qt-installer
cratedb
cairo
off-canvas-menu
jcl
raphael
cruisecontrol.net
richfaces
jquery-scrollify
bootstrap-typeahead
bitcoin-testnet
autofill
blackboard
geomesa
flex4.5
jrules
startapp
windows-firewall
nstouchbar
jags
sgmlreader
goquery
crash-reports
android-bitmap
volume
fontconfig
opencpu
rxvt
festival
sqldatareader
coremidi
pydio
foxit
mercurial-hook
amazon-kcl
fill
yoothemes
approval-tests
datainputstream
textkit
windowlistener
abas
connection-reset
findcontrol
orientation-changes
viewflipper
mov
late-static-binding
opensocial
reserved-words
android-json-rpc
java.util.date
windows-mobile-6
nservicebus4
sslexception
onselect
gridworld
iconv
pep8
client-library
fluidsynth
icsharpcode
crocodoc
returnurl
subtract
rose-db-object
text-services-framework
makefile-project
law-of-demeter
server-error
email-spec
winverifytrust
internals
visual-studio-dbpro

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