Skip to content Skip to sidebar Skip to footer

How To Stop Writing A Blank Line At The End Of Csv File - Pandas

When saving the data to csv, data.to_csv('csv_data', sep=',', encoding='utf-8', header= False, index = False), it creates a blank line at the end of csv file. How do you avoid th

Solution 1:

One way would be to save data except the last entry,with default line_terminator(\n) and append the last line with line_terminator="" .

data1 = data.iloc[0:len(data)-1]
data2 = data.iloc[[len(data)-1]]
data1.to_csv('csv_data', sep=',', encoding='utf-8', header= False, index = False)
data2.to_csv('csv_data', sep=',', encoding='utf-8', header= False, index = False,mode='a',line_terminator="")

Solution 2:

For some reason, the line terminator did not work when I tried it. (It gave an error, saying line_terminator is an unrecognized keyword argument.)

However, this will do the trick:

    df.to_csv(path)
    with open(path) as f:
        lines = f.readlines()
        last = len(lines) - 1lines[last] = lines[last].replace('\r','').replace('\n','')
    with open(path, 'w') as wr:
        wr.writelines(lines)

Solution 3:

file_out = r'c:\your_output_file_path\file_name.csv'
df.to_csv(file_out)
file_data = open(file_out, 'rb').read()
open(file_out, 'wb').write(file_data[:-2])

df.to_csv() function has a parameter called line_terminator with a default value of '\n'. This new line character is the issue at hand.

The code above: 1) writes the dataframe to file as normal 2) opens the file and reads in the bytes data to the file_data variable 3) writes the file_data variable back out to the same file but trims off the '\n' with the splice: file_data[:-2]

Solution 4:

One solution is to not use pandas to export the data to a file. The example below will not include an empty row at the end of the file. However, it's probably a lot slower than pandas' "to_csv" method.

import pandas as pd

def export_dataframe_to_file( 
        df: pd.DataFrame, file_name: str, 
        header=True, index=True, delimiter=',',
        line_terminator='\n', encoding='utf-8' 
        ) ->None:
    '''
    This function exports a Pandas DataFrame to a file without
    including an empty row at the very end of the file.
    '''
    number_of_rows, current_row= len(df), 1withopen(file_name, 'w', encoding=encoding) as file :
        if header:
            file.write( 
                delimiter*index + delimiter.join(df.columns) \
                + line_terminator 
                )
        for df_index, series in df.iterrows():
            file.write( 
                (str(df_index) + delimiter)*index \
                + delimiter.join(series.astype( str )) \
                + line_terminator*(notnot number_of_rows -current_row)
                )
            current_row+=1return

Solution 5:

None of the solutions above worked because as the original question asked he was trying to send the file to another script/REST API that wouldn't accept carriage return. This is likely caused by the requests library he is using to send the csv file to the REST API. I was able to use the requests library to send a file which had a carriage return via REST API:

import requests
import pandas as pd
import settings


file_name = Hierarchy.csv'
df = pd.read_csv(file_name)
df.to_csv(file_name, sep=',', encoding='utf-8', index=False)
headers = {
  'x-api-key': settings.MONITOR_REST_API_KEY,
  'x-api-token': settings.MONITOR_REST_API_TOKEN,
  'accept': 'application/json'
}

files = {'file': (file_name, open(file_name, 'rb'), 'text/csv')}
monitor_rest_url = "https://api.yourcloud.com"
response = requests.post(monitor_rest_url +'/api/v2/your_endpoint',
                        files=files, verify=False, headers=headers)
print(response.text)

Post a Comment for "How To Stop Writing A Blank Line At The End Of Csv File - Pandas"