Python Forum
Cut .csv to pieces and save as .csv
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cut .csv to pieces and save as .csv
#16
Hi Merlem and all the other folks!

Ok, so this is as far as I am.

Your script is cutting the list to the right pieces. Only the naming of the files is not quite right yet. I sort of need "518a", "518b", "519a", "519b".....

Since I do realize that this might be a bit too challenging for a newbie, I would really like to ask some Questions:

import csv

f = open("/home/herbert/PycharmProjects/2017-02-10 CutList/Kastanie_Jahrringe.csv")
csv_f = csv.reader(f, delimiter=';')

firstlinedone = False <= why this? Probably starting parameter, right?
listoffiles = [] <= make a empty list of files, right?
for line in csv_f: <= cycle through every value of the csv

   # for the first line, make all files <= how to get 1a, 1b, 2a, 2b, 3a, 3b etc.  maybe a List with two objects a and b?
   if firstlinedone == False: <= as long as the first line goes on keep going

       for columnumber, column in enumerate(line): <= now you're loosing me: columnnumber AND column? And enumerate = count lines?

           newfilename = "Baumprobe" + str(columnumber) + ".csv" <= now im on the road again... I like this stuff
           # one can also do more formatting here for the filename

           newfile = open(newfilename, "w") <= open new files in mode write which means create files

           listoffiles.append(newfile) <= always append the next new file to listoffiles

           firstlinedone == True <= if however, you are done with the first line, then:

   for columnumber, column in enumerate(line): <= still not getting this line

       csvwriter = csv.writer(listoffiles[columnumber], delimiter=',') <= i "sort of" get this, but not in detail: define csvwriter as a method which takes the columns and cuts them by delimiter?

       csvwriter.writerow([line[0], line[columnumber]]) <= why now write again?

for anyfile in listoffiles: <= go through all the objects in listoffiles and close them
   anyfile.close()
f.close() <= close all functions
[color=#a9b7c6][font=Source Code Pro]
[/font][/color]

(Feb-16-2017, 11:03 AM)buran Wrote: Check PyCharm settings - that the project is set up to use the correct interpreter (the one with pandas installed). Linux comes with Python pre-installed and if you installed also Anaconda, I would assume you have at least 2 (and most probably 3) Python installations

Also, my other snippet does not use pandas, just the csv module.

thanks - I will check on that.

Via PyCharm settings it only shows me python 2.7 (not selected) and python 3.5 (selected) but im sure ill make it work if I try on!

(Feb-16-2017, 11:03 AM)buran Wrote: Also, my other snippet does not use pandas, just the csv module.



Thank you - I will test the script and will give you feedback on how its working!

PyCharm seems to be working with Anaconda (python 3.6) now.

On your script it gives me:


Error:
Traceback (most recent call last):   File "/home/herbert/Pycharm_Projekte/2017-02-10 CutList/CutList_Buran.py", line 5, in <module>     filenames = zip(csv_f.fieldnames, 'ab' * (len(csv_f.fieldnames) / 2)) TypeError: can't multiply sequence by non-int of type 'float'
Im guessing python does'nt like float as a result of len/2.....?
Reply


Messages In This Thread
RE: Cut .csv to pieces and save as .csv - by wavic - Feb-13-2017, 09:29 AM
RE: Cut .csv to pieces and save as .csv - by merlem - Feb-13-2017, 09:48 AM
RE: Cut .csv to pieces and save as .csv - by buran - Feb-13-2017, 09:31 AM
RE: Cut .csv to pieces and save as .csv - by wavic - Feb-13-2017, 10:13 AM
RE: Cut .csv to pieces and save as .csv - by merlem - Feb-13-2017, 11:14 AM
RE: Cut .csv to pieces and save as .csv - by merlem - Feb-13-2017, 12:54 PM
RE: Cut .csv to pieces and save as .csv - by buran - Feb-13-2017, 03:05 PM
RE: Cut .csv to pieces and save as .csv - by merlem - Feb-13-2017, 08:15 PM
RE: Cut .csv to pieces and save as .csv - by buran - Feb-15-2017, 04:34 PM
RE: Cut .csv to pieces and save as .csv - by buran - Feb-16-2017, 11:03 AM
RE: Cut .csv to pieces and save as .csv - by BruderKellermeister - Feb-16-2017, 11:58 AM
RE: Cut .csv to pieces and save as .csv - by buran - Feb-16-2017, 12:11 PM
RE: Cut .csv to pieces and save as .csv - by merlem - Feb-16-2017, 12:22 PM
RE: Converting a bunch of .csv to .txt - by buran - May-04-2017, 10:31 AM
RE: Converting a bunch of .csv to .txt - by buran - May-04-2017, 12:12 PM
RE: Converting a bunch of .csv to .txt - by Kebap - May-04-2017, 12:34 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020