Jump to content


From Csv To Textfile

  • Please log in to reply
No replies to this topic

#1 Herr Fledermaus

Herr Fledermaus

    New Member

  • Members
  • Pip
  • 1 posts

Posted 11 May 2011 - 05:07 AM

Hi all,

I really hope someone can help me out here. I have this csv (comma seperated value) file that I need to manipulate and convert to a txt. I tried the approach as in our book on page 404 but now I'm stuck. Totally. Lost in space.


row_data = {}

with open(sourceFile) as myCounter:
    column_headings = myCounter.readline().strip().split(',')

    for each_line in myCounter:
        row = each_line.strip().split(',')
        row_label = row.pop(0)
        row_data[row_label] = row

num_cols = len(column_headings)
print(num_cols, end=' -> ')

The main goal of my try-out is/was:

1. read the headings of a csv-file (the first row, that is)
2. read the rest of the file and put it in a dictionary (like in mylist)
3. write some (not all) fields of the records to a file, each field on it's own line.


input csv-file:
fledermaus, 555, fledermaus@nowhere.com
herr, 556, herr@nowhere.com

output text-file:
name: fledermaus
email: fledermaus@nowhere.com
name: herr
email: herr@nowhere.com

So that's basically what I want to do. Can someone help me on my way please? Meanwhile, I'll restudy some chapters in the book, I guess.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users