2

I tried lot of suggestions but I am unable to remove carriage returns. I am new python and trying it with csv file cleaning.

import csv filepath_i = 'C:\Source Files\Data Source\Flat File Source\PatientRecords.csv' filepath_o = 'C:\Source Files\Data Source\Flat File Source\PatientRecords2.csv' rows = [] with open(filepath_i, 'rU', newline='') as csv_file: #filtered = (line.replace('\r\n', '') for line in csv_file) filtered = (line.replace('\r', '') for line in csv_file) csv_reader = csv.reader(csv_file, delimiter=',') i = 0 for row in csv_reader: print(row) i = i + 1 if(i == 10): break #with open(filepath_o, 'w',newline='' ) as writeFile: # writer = csv.writer(writeFile,lineterminator='\r') # for row in csv_reader: # #rows.append(row.strip()) # rows.append(row.strip()) # writer.writerows(rows) 

Input

DRG Definition,Provider Id,Provider Name,Provider Street Address,Provider City,Provider State,Provider Zip Code,Hospital Referral Region Description,Hospital Category,Hospital Type, Total Discharges ,Covered Charges , Total Payments ,Medicare Payments 039 - EXTRACRANIAL PROCEDURES W/O CC/MCC,10001,SOUTHEAST ALABAMA MEDICAL CENTER,1108 ROSS CLARK CIRCLE,DOTHAN,AL,36301,AL - Dothan,Specialty Centers,Government Funded,91,"$32,963.07 ","$5,777.24 ","$4,763.73 " 039 - EXTRACRANIAL PROCEDURES W/O CC/MCC,10005,MARSHALL MEDICAL CENTER SOUTH,"2505 U S HIGHWAY 431 NORTH",BOAZ,AL,35957,AL - Birmingham,Specialty Centers,Private Institution,14,"$15,131.85 ","$5,787.57 ","$4,976.71 " 039 - EXTRACRANIAL PROCEDURES W/O CC/MCC,10006,ELIZA COFFEE MEMORIAL HOSPITAL,205 MARENGO STREET,FLORENCE,AL,35631,AL - Birmingham,Rehabilitation Centers,Private Institution,24,"$37,560.37 ","$5,434.95 ","$4,453.79 " 

Output (4th column 'Provider Street Address')

['DRG Definition', 'Provider Id', 'Provider Name', 'Provider Street Address', 'Provider City', 'Provider State', 'Provider Zip Code', 'Hospital Referral Region Description', 'Hospital Category', 'Hospital Type', ' Total Discharges ', 'Covered Charges ', ' Total Payments ', 'Medicare Payments'] ['039 - EXTRACRANIAL PROCEDURES W/O CC/MCC', '10001', 'SOUTHEAST ALABAMA MEDICAL CENTER', '1108 ROSS CLARK CIRCLE', 'DOTHAN', 'AL', '36301', 'AL - Dothan', 'Specialty Centers', 'Government Funded', '91', '$32,963.07 ', '$5,777.24 ', '$4,763.73 '] ['039 - EXTRACRANIAL PROCEDURES W/O CC/MCC', '10005', 'MARSHALL MEDICAL CENTER SOUTH', '2505 U S HIGHWAY \n431 NORTH', 'BOAZ', 'AL', '35957', 'AL - Birmingham', 'Specialty Centers', 'Private Institution', '14', '$15,131.85 ', '$5,787.57 ', '$4,976.71 '] 
8
  • did you try strip() ? Commented Aug 1, 2019 at 15:25
  • ya I tried but it didn't work. Commented Aug 1, 2019 at 15:29
  • can u try with pandas and read_csv function in pandas ? Commented Aug 1, 2019 at 15:31
  • 1
    It worked with pandas, but my target application is not supporting external package. so I am trying with base methods. Commented Aug 1, 2019 at 15:34
  • csv.reader(csv_file, delimiter=',', quotechar='"') <- this would read csv properly, and then you can handle the linebreaks as usual Commented Aug 1, 2019 at 15:48

1 Answer 1

3

I ran this on my side and it works:

with open(filepath_i, 'rU', newline='') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') for row in csv_reader: row[3] = row[3].replace("\n","").replace("\r","") print(row) 

Output:

['DRG Definition', 'Provider Id', 'Provider Name', 'Provider Street Address', 'Provider City', 'Provider State', 'Provider Zip Code', 'Hospital Referral Region Description', 'Hospital Category', 'Hospital Type', ' Total Discharges ', 'Covered Charges ', ' Total Payments ', 'Medicare Payments'] ['039 - EXTRACRANIAL PROCEDURES W/O CC/MCC', '10001', 'SOUTHEAST ALABAMA MEDICAL CENTER', '1108 ROSS CLARK CIRCLE', 'DOTHAN', 'AL', '36301', 'AL - Dothan', 'Specialty Centers', 'Government Funded', '91', '$32,963.07 ', '$5,777.24 ', '$4,763.73 '] ['039 - EXTRACRANIAL PROCEDURES W/O CC/MCC', '10005', 'MARSHALL MEDICAL CENTER SOUTH', '2505 U S HIGHWAY 431 NORTH', 'BOAZ', 'AL', '35957', 'AL - Birmingham', 'Specialty Centers', 'Private Institution', '14', '$15,131.85 ', '$5,787.57 ', '$4,976.71 '] ['039 - EXTRACRANIAL PROCEDURES W/O CC/MCC', '10006', 'ELIZA COFFEE MEMORIAL HOSPITAL', '205 MARENGO STREET', 'FLORENCE', 'AL', '35631', 'AL - Birmingham', 'Rehabilitation Centers', 'Private Institution', '24', '$37,560.37 ', '$5,434.95 ', '$4,453.79 '] 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.