Python Forum
CSV import results in listing of all letters of elements - 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: CSV import results in listing of all letters of elements (/thread-3434.html)



CSV import results in listing of all letters of elements - Bigshow23 - May-23-2017

My bank statement is a CSV with two different delimiters namely '|' and ’;' . I wanted to write a script, that makes me a 2-dimensional list (lines+rows) of the values to be able to put the values in the same order in every row – some lines (where tings are paid in other currencies have an additional row[2] as you can see in the first line in my testfile – the lines vary in length.
I wanted to delete this elements in order to have a nice CSV with the correct values under each other.

######Testfile##############
XY234567890123223423;INSURANCE Corpxy |USD 8,42|74921027234234523507320;03.04.2017;28.03.2017;-8,42;EUR
XY234567890123223423;AIRBNB * HM4R3TCPM 332-048-3753|743133023423423400822139045;03.04.2017;29.03.2017;-67,00;EUR


import csv, pprint

fpath = './Testlist.csv'

def readfile():
    records = []
    with open(fpath, 'r') as originalfile:
        rawdata1 = originalfile.read()
        print(rawdata1)

        rawdata1 = str(rawdata1).replace("|",";")
        print(rawdata1)
        csvreader = csv.reader(rawdata1, delimiter=';')
        for row in csvreader:
            records.append(row)
        print(records)
def printlist(records):
    for elem in records:
        print(elem)

records = readfile()



RE: CSV import results in listing of all letters of elements - buran - May-23-2017

what the desired output should look like?
in general case you need to read the file line by line, split at |, then split first and last element at ; (or try to split all of the elements at ;)


RE: CSV import results in listing of all letters of elements - Bigshow23 - May-23-2017

Thanks for the answer so far, the output should look like this:
XY234567890123223423;INSURANCE Corpxy               ;74921027234234523507320    ;03.04.2017;28.03.2017;- 8,42;EUR 
XY234567890123223423;AIRBNB * HM4R3TCPM 332-048-3753;743133023423423400822139045;03.04.2017;29.03.2017;-67,00;EUR
why does Python read each character as list in my code - look here: [['X'], ['Y'], ['2'], ['3'],.....] ?
Why can't I use the CSV module?
Thx


RE: CSV import results in listing of all letters of elements - buran - May-23-2017

you can use csv if you wish

import csv


with open('testlist.csv', 'r') as f:
    csvreader = csv.reader(f, delimiter='|')
    for row in csvreader:
        print(';'.join((row[0], row[-1])))
or

import csv

def parse_data(row):
    return ';'.join((row[0], row[-1]))

with open('testlist.csv', 'r') as f:
    csvreader = csv.reader(f, delimiter='|')
    bank_data = map(parse_data, csvreader)
    print('\n'.join(bank_data))
Output:
XY234567890123223423;INSURANCE Corpxy ;74921027234234523507320;03.04.2017;28.03.2017;-8,42;EUR XY234567890123223423;AIRBNB * HM4R3TCPM 332-048-3753;743133023423423400822139045;03.04.2017;29.03.2017;-67,00;EUR