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

Django-allauth - URL isn't included in the app's domains
counting paths for a given situation
Iterative version of this recursive function
Python: Open CSV and convert first column from Unix Timestamp to human date
Python Multiclient Multithreaded server using sockets
Connect to Flask application via public IP [duplicate]
odeint usage: Solving a transient heat transfer + chemical reaction model in Python
Python automatic web test tool with selenium: download file in firefox
merge pandas dataframe with key duplicates
Threads real-time logging
Fill Enum in function Python
Send data from C Parent to Python Child and back using a Pipe
AES cipher process creation
Python list object is not callable for a list of integers [duplicate]
What does the following error message mean for an inverse matrix?
Python3/SQLite3 | How to create multiple tables from a list or list of tuples?

Categories

HOME
paypal
spring
ajax
sass
project-management
rotation
h2
automated-tests
recyclerview
diagram
x11
uiview
cosmicmind
postmessage
dendrogram
redux-observable
zend-framework-mvc
ip-camera
plupload
kitematic
jquery-scrollify
activepython
codelite
frp
i3
hawtio
body-parser
powershell-remoting
xen
es-shell
active-model-serializers
jquery-multidatespicker
onbackpressed
dart-pub
excel-interop
tango
catia
jags
slot
pluck
quintus
paas
uicollectionviewlayout
collapsingtoolbarlayout
cjson
scalar
coordinate-transformation
spring-lemon
mongo-c-driver
asp.net-mvc-partialview
sun-codemodel
asp.net-mvc-2
license-key
plone-3.x
mysqldumpslow
python-winshell
ffserver
pgm
jazz
moai
kendo-mvvm
log4cplus
smartystreets
xenocode
baucis
confusion-matrix
visual-studio-2003
cgimageref
template-haskell
android-contextmenu
enumerators
usn
couchdb-lucene
tfs-power-tools
wchar
loadui
hadoop-plugins
scala-2.8
visual-c++-2008-express
odbc-sql-server-driver
opcodes
system-codedom-compiler
virtualquery

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