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
#9
(Feb-13-2017, 12:07 PM)BruderKellermeister Wrote: when trying to run your code from above I get:


csvwriter.writerow([line[0], line[columnumber]])

TypeError: a bytes-like object is required, not 'str'

That's strange. For me, the code runs without this error. I'm not sure whether that's relevant in this case, but I am using python in the version 2.7. If you have version 3, that can cause incompatibilities.
And I have to confess that I don't understand this message... the expression in the brackets should be a list, not a string. But 'a bytes-like object', I'm not sure what is included in that and what isn't.
Maybe somone else can help there?

According to the code: you may try to replace the old 'firstlinedone'-part with:
        for columnumber, column in enumerate(line):
            newfilename = "test" + str(column) + "a.csv"
            if newfilename in listoffilenames:
                newfilename = "test" + str(column) + "b.csv"
                listoffilenames.append(newfilename)
            else:
                listoffilenames.append(newfilename)
            newfile = open(newfilename, 'wb')
            listoffiles.append(newfile)
            firstlinedone = True
Quote:Any ideas on how to approach getting better at this?
Not really, except maybe: try to get stuff done that you need for something else. It keeps up motivation  Wink  . But learning programming remains hard and difficult for a long time. Don't give up too easily!
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 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