简体   繁体   中英

Trouble Reading a csv data file in python

I am not able to read a CSV data file in my environment.

I have tried the following to read the data

df = pd.read_csv('AC.csv', delimiter =";", 
                  engine='python')
df.head()

I want to read the following CSV into my code "";"date_start_balance";"date_end_balance";"booking_date";"start_balance";"end_balance";"amount";"booking_text";"tx_code" "1";"22.02.16";"22.03.16";"22.03.16";35908,38;35898,38;-10;"GEBUEHREN";"808" "2";"22.02.16";"22.03.16";"22.03.16";697,88;687,88;-10;"GEBUEHREN";"808" "3";"15.03.16";"21.03.16";"21.03.16";45216,5;46201;520;"SEPA-Gutschrift";"166" "4";"15.03.16";"21.03.16";"21.03.16";45216,5;46201;464,5;"SEPA-Gutschrift";"166" "5";"17.03.16";"22.03.16";"22.03.16";30616,1;30606,1;-10;"GEBUEHREN";"808" "6";"17.03.16";"21.03.16";"21.03.16";18363,55;19562,55;541;"SEPA-Gutschrift";"166" "7";"17.03.16";"21.03.16";"21.03.16";18363,55;19562,55;279;"SEPA-Gutschrift";"166" "8";"17.03.16";"21.03.16";"21.03.16";18363,55;19562,55;199;"SEPA-Gutschrift";"166" "9";"17.03.16";"21.03.16";"21.03.16";18363,55;19562,55;180;"SEPA-Gutschrift";"166" "10";"18.03.16";"21.03.16";"21.03.16";150072,92;143245,79;100;"SEPA Rueckgabe";"159" "11";"18.03.16";"21.03.16";"21.03.16";150072,92;143245,79;-6511,68;"SEPA-Ueberweisungsdatei";"191" "12";"18.03.16";"21.03.16";"21.03.16";150072,92;143245,79;-308,5;"SEPA-Ueberweisungsdatei";"191" "13";"18.03.16";"21.03.16";"21.03.16";150072,92;143245,79;-106,95;"SEPA Basislastschrift";"105" "14";"18.03.16";"22.03.16";"22.03.16";2924651,6;1924641,6;-1e+06;"SEPA-Ueberweisung";"116" "15";"18.03.16";"22.03.16";"22.03.16";2924651,6;1924641,6;-10;"GEBUEHREN";"808" "16";"18.03.16";"21.03.16";"21.03.16";1294633,32;1315175,93;1644;"AUSSENH.";"202" "17";"18.03.16";"21.03.16";"21.03.16";1294633,32;1315175,93;1320;"SEPA-Gutschrift";"166" "18";"18.03.16";"21.03.16";"21.03.16";1294633,32;1315175,93;1076,03;"SEPA-Gutschrift";"166" "19";"18.03.16";"21.03.16";"21.03.16";1294633,32;1315175,93;1049,89;"SEPA-Gutschrift";"166" "20";"18.03.16";"21.03.16";"21.03.16";1294633,32;1315175,93;1047;"SEPA-Gutschrift";"166" "21";"18.03.16";"21.03.16";"21.03.16";1294633,32;1315175,93;867,99;"SEPA-Gutschrift";"166" " 22";"18.03.16";"21.03.16";"21.03.16";1294633,32;1315175,93;849;"SEPA-Gutschrift";"166" "23";"18.03.16";"21.03.16";"21.03.16";1294633,32;1315175,93;799;"SEPA-Gutschrift";"166"

Try cleaning your CSV file first. Ie

# example line is...
# line = '4;"15.03.16";"21.03.16";"21.03.16";45216    5;46201;464 5;"XYZ";"166"'
with open("something.cleaned.csv", "w") as outfile:
    with open("something.csv", "r") as infile:
        for line in infile:
            ### CHOOSE ONE ###
            line = line.replace(" ","").replace("\t", "") # If you want  21.03.16  452165  46201
            # OR
            line = ';'.join(line.split()) # If you want  21.03.16  45216  5  46201
            ##################
            outfile.write(line)

Below

import pandas as pd


def _handle_space(x):
    return int(x.replace(' ', ''))


df = pd.read_csv('x.csv', delimiter=';', converters={4: _handle_space, 6: _handle_space}, header=None)
print(df)

output

  0         1         2         3       4      5     6    7    8
0  4  15.03.16  21.03.16  21.03.16  452165  46201  4645  XYZ  166

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM