Python Forum
CSV import results in listing of all letters of elements
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSV import results in listing of all letters of elements
#1
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()
Reply
#2
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 ;)
Reply
#3
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
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Listing directories (as a text file) kiwi99 1 802 Feb-17-2023, 12:58 PM
Last Post: Larz60+
  Read directory listing of files and parse out the highest number? cubangt 5 2,251 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  ValueError: Length mismatch: Expected axis has 8 elements, new values have 1 elements ilknurg 1 5,013 May-17-2022, 11:38 AM
Last Post: Larz60+
  Listing All Methods Of Associated With A Class JoeDainton123 3 2,296 May-10-2021, 01:46 AM
Last Post: deanhystad
  Sorting Elements via parameters pointing to those elements. rpalmer 3 2,549 Feb-10-2021, 04:53 PM
Last Post: rpalmer
  Listing files with glob. MathCommander 9 4,814 Oct-26-2020, 02:04 AM
Last Post: MathCommander
  Listing data from a list ebolisa 1 1,707 Sep-29-2020, 02:24 PM
Last Post: DeaD_EyE
  Listing Attributes of Objects & Classes JoeDainton123 4 2,317 Aug-28-2020, 05:27 AM
Last Post: ndc85430
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,190 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Importing module from a package results in import error goghvv 2 2,340 Mar-27-2020, 07:13 PM
Last Post: goghvv

Forum Jump:

User Panel Messages

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