Python Forum
hi guys, i'm back to learning python !
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hi guys, i'm back to learning python !
#1
i'm trying to take a .CSV file and write its data into another file,
i don't wanna copy the file, if you'll look at my code you'll probably see what i'm trying to do...

here's the code:
import csv

file_input = '/home/tal/code/AAPL.csv'
file_output = '/home/tal/code/file_output.txt'

with open(file_input, 'r') as csv_file:

    reader = csv.reader(csv_file)

    for line in reader:
        with open(file_output, 'w') as file_op:
            file_op.write(line)
this is the ouput when i debug it:

Output:
Traceback (most recent call last): File "/home/tal/.config/JetBrains/PyCharmCE2020.2/scratches/scratch.py", line 12, in <module> file_op.write(line) TypeError: write() argument must be str, not list
but i don't know how to convert the CSV data into a string...
i checked allot of websites, no where to find a way...

I appreciate your help

Tal
Reply
#2
str has a join method that will concatenate the items of an iterable (docs here), e.g.:

>>> ", ".join(["foo", "bar", "baz"])
'foo, bar, baz'
Reply
#3
Don't use csv if you want to read strings. You don't have to use csv to read something that is formatted as CSV. Only use csv if you want to split input lines into columns.
Reply
#4
thanks to both of you !
Reply
#5
There is some low level stuff you can let csv module to handle. For example, if there is a file with one row: "AAPL", 113.02 then we can leave handling of "" and conversion to csv module:

import csv


with open('aapl.csv', 'r') as f:
    reader = csv.reader(f, quoting=csv.QUOTE_NONNUMERIC)
    print(*reader)

# -> ['AAPL', 113.02]


with open('aapl.csv', 'r') as f:
    data = [item for row in f for item in row.strip().split(', ')]
    print(data)

# -> ['"AAPL"', '113.02']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
if you are using csv module for reading, just use it also for writing
or not use csv module, just read a line, write a line
or you can also look at pandas which has methods to read and write csv files
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
thank you so much !
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  front-end js and back-end python 3lnyn0 0 988 Jun-03-2022, 08:51 PM
Last Post: 3lnyn0
  guys please help me , pycharm is not able to identify my xlsx file CrazyGreenYT7 1 2,009 Jun-13-2021, 02:22 PM
Last Post: Larz60+
  Virtual Assistant code is looping back in python ShishirModi 1 2,519 Oct-13-2020, 08:04 AM
Last Post: Nickd12
  Hi Guys, please help me to write SAS macro parameter equivalent code in Python Manohar9589 2 2,588 Jun-14-2020, 05:07 PM
Last Post: Larz60+
  Could you guys help with this bug. I would post on StackOverflow but I got tempbanned ryder5227 2 2,376 Sep-26-2019, 08:01 PM
Last Post: metulburr
  Help Please guys AbuSiraj 2 2,677 Feb-14-2019, 09:29 AM
Last Post: AbuSiraj
  Hey guys, help a newbie out with a list problem. sdezigner 2 2,668 Feb-15-2018, 06:06 PM
Last Post: sdezigner
  Hello,Guys!,I met a very strange bug homepoeple 4 3,297 Jan-26-2018, 12:16 PM
Last Post: homepoeple
  Hey guys can you please help me with some basic coding Stabu 9 6,255 Jul-16-2017, 10:11 AM
Last Post: MyImpsNamesAre

Forum Jump:

User Panel Messages

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