Python Forum
Can anyone please help with very simple code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can anyone please help with very simple code
#1
Hi, new to the forums here, ive joined to seek help on this single matter. Im not a pythonista but know i have it installed on my linux box.
Please could someone help me do the following simple task:

Open an existing csv file.

read each cell of column 2 (7770 items).

create a text file.

append a "," to each word from the csv and then append it to the text file.

close file

Please help, thankyou in advance :)
Reply
#2
csv tutorial
https://pymotw.com/2/csv/
Recommended Tutorials:
Reply
#3
Regardless if this is for homework or just work, we don't write solutions.  Here, we help people learn, by fixing whatever mistakes/errors happen to be in their code.  So give it a try, and we'll help along the way.

But we won't do it for you.
Reply
#4
that should be our motto
Recommended Tutorials:
Reply
#5
"Bug fixes before solutions"
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
Ok thanks.
I have got this far :
import csv, os

ef = open('towns.csv')
er = csv.reader(ef)
ed = list(er)

print (ed)
how do i print out as a string with a comma seperating the items please, this is not homework.

update :
#! python2
# removeCsvHeader.py - Removes the header from all CSV files in the current
# working directory.

import csv, os

ef = open('towns.csv')
er = csv.reader(ef)
ed = list(er)



path = 'towns.txt'
towns = open(path,'w')
ni = towns.write(str(ed))
print (ed)
towns.close()
ef.close()
now its writing but also writing the brackets and single quotes around list items : ['item']
Reply
#7
You want the join method of strings, that will combine them with commas for printing out. You would normally do it in a loop:

for line in data:
    out_file.write(','.join(line) + '\n')
Or you could do a double join on the comma and the newline.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  First Day using Python. NEED Simple Math CODE HELP! Jesseluke 4 1,396 Jan-13-2023, 01:04 PM
Last Post: jefsummers
  my simple code wont work! help! simon12323121 2 2,043 Sep-05-2021, 09:06 AM
Last Post: naughtyCat
  i need help with this simple code josephb401 12 12,888 Oct-18-2016, 10:25 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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