Python Forum
Load and format a CSV file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Load and format a CSV file (/thread-22076.html)

Pages: 1 2


Load and format a CSV file - fioranosnake - Oct-28-2019

HI,

I have a csv file with a single 'column'

Code
ABC
DEF
GHI
JKL
MNO


Is it possible to read and format this into a single string placing an ID before each row eg:

{1:ABC,2:DEF,3:GHI,4:JKL,5:MNO}

Thanks for any advice


RE: Load and format a CSV file - perfringo - Oct-28-2019

I just observe that {1:ABC,2:DEF,3:GHI,4:JKL,5:MNO} is not string.


RE: Load and format a CSV file - snippsat - Oct-28-2019

(Oct-28-2019, 05:35 PM)fioranosnake Wrote: Is it possible to read and format this into a single string placing an ID before each row eg:
Yes,you should show what you have tried.
Here is a big hint,and it look more like a dictionary than a string Wink
>>> data = '''\
... ABC
... DEF
... GHI
... JKL
... MNO'''.split('\n')

>>> data
['ABC', 'DEF', 'GHI', 'JKL', 'MNO']

>>> dict(zip(list(range(1,6)), data))
{1: 'ABC', 2: 'DEF', 3: 'GHI', 4: 'JKL', 5: 'MNO'}



RE: Load and format a CSV file - Gribouillis - Oct-28-2019

There is also
>>> dict(enumerate(data, 1))



RE: Load and format a CSV file - fioranosnake - Oct-29-2019

(Oct-28-2019, 07:23 PM)Gribouillis Wrote: There is also
>>> dict(enumerate(data, 1))

Hi, Thanks for this response. This only appears to format the first row (headings)... can it skip this and perform the formatting on all other rows..?


RE: Load and format a CSV file - snippsat - Oct-29-2019

Post a sample on how the raw csv file look like without edit.
We can only go from what on what you did show in first post.
fioranosnake Wrote:I have a csv file with a single 'column'

Code
ABC
DEF
GHI
JKL
MNO



RE: Load and format a CSV file - fioranosnake - Oct-29-2019

Hi,

If I open my txt file in notepad it looks as follows :


1231241231
1235135135
5457345345
4577865568
8654563848

and so on.

If I format the text file to look like :

1231241231,1235135135,5457345345,4577865568,8654563848

Then the solutions you have suggested work perfectly..

Do I need to carry out some formatting beforehand when the txt file is read?

Thanks again


RE: Load and format a CSV file - snippsat - Oct-29-2019

fioranosnake Wrote:Do I need to carry out some formatting beforehand when the txt file is read?
No look straight forward if you file look like that.
from pprint import pprint

with open('numb.csv') as f:
    #next(f) #If need to skip header
    data = [i.strip() for i in f]
    pprint(dict(enumerate(data, 1)))
Output:
{1: '1231241231', 2: '1235135135', 3: '5457345345', 4: '4577865568', 5: '8654563848'}



RE: Load and format a CSV file - fioranosnake - Oct-29-2019

Hi, I there a straight forward way to write the pprint output to a text file?

Thanks


RE: Load and format a CSV file - snippsat - Oct-29-2019

(Oct-29-2019, 09:29 PM)fioranosnake Wrote: Hi, I there a straight forward way to write the pprint output to a text file?
with open('numb.csv') as f,open('out.csv', 'w') as f_out:
    #next(f) # If need to skip header
    data = [i.strip() for i in f]
    data = dict(enumerate(data, 1))
    for k,v in data.items():
        f_out.write(f'{k}: {v}\n')
Output:
1: 1231241231 2: 1235135135 3: 5457345345 4: 4577865568 5: 8654563848