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
#21
Hi folks,


i have a bunch of tab delimited .csv's - which is nice.

But now my coworker is telling me, he needs all theese .csv's as .txt since his special Software only accepts .txt.

I hope this can't be too dificult, since both formats are textfiles...?

The resulting files have to be "tab delimited"...

How should i approach this task?

here is the code i've gotten my .csv files from (thanks to the folks here that helped me):

import pandas as pd

df = pd.read_csv('/home/Cutting Excel/Kastanie_Jahrringe.csv', sep=';')
for col in df.columns[1:]:
   if col.endswith('.1'):
       fname = '/home/Cutting Excel/Cut Pieces/{}.csv'.format(col.replace('.1', 'b'))
   else:
       fname = '/home/Cutting Excel/Cut Pieces/{}a.csv'.format(col)
   df.to_csv(fname, sep='\t', columns=['YEAR', col], index=False, na_rep='NA',
             header=['YEAR', col.replace('.1', '')])
Reply
#22
well, just change the extension in your output file from csv to txt. Anyway in the output file you use '\t' as separator.
Reply
#23
ok thanks, ill try that.

I will use the codeblock for posting code from now on, but somehow, when i tried in the post above, all the code got messed up (because its formatted with colors i guess...?).
Reply
#24
use Ctrl+Shift+V to paste the code or check BBcode help for more info how to remove the formatting before you submit the post.
Reply
#25
(May-04-2017, 09:41 AM)BruderKellermeister Wrote: How should i approach this task?

here is the code i've gotten my .csv files from (thanks to the folks here that helped me):

I will merge this thread from 2 hours ago with your other thread about the same issue with your last post from 5 hours ago.
Reply


Forum Jump:

User Panel Messages

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