unable to write content in csv filw

Cameron Simpson cs at cskk.id.au
Mon Apr 27 18:20:00 EDT 2020


1: Does the code run to completion without errors?
   If there's an exception then the data may not get to the file because 
   I/O is normally buffered in memory and written out in larger chunks 
   as the buffer fills i.e. at a time later than your .writerow() call.

2: If you delete the CSV file before running your code, does it get 
recreated? If not, maybe your filename is incorrect.

As a matter of practice you should write filenames using "raw" strings.  
Instead of writing this:

    with open("D:\PHD\obranking\\test_chi.csv", 'w') as csvfilew1:

it is better to write this:

    with open(r'D:\PHD\obranking\test_chi.csvr', 'w') as csvfilew1:

In a raw string the backslash is not special and does not need to be 
doubled. More importantly, the reason the backslash is special is that 
in ordinary string syntax it is used to introduce various special 
characters, such as "\n" to specify a newline character.

If you have an unfortunate filename such as:

    D:\PHD\new_files

then you might easily produce a string with an embedded newline instead 
of a file separator and an "n". Likewise for the various other escapes.

With a raw string like:

    r'D:\PHD\new_files'

you do not run this risk.

Cheers,
Cameron Simpson <cs at cskk.id.au>

On 27Apr2020 09:14, Rahul Gupta <rahulgupta100689 at gmail.com> wrote:
>FOLLWOING IS MY CODE
>import pandas as pd
>import csv
>from sklearn.preprocessing import LabelEncoder
>from sklearn.feature_selection import chi2
>with open("D:\PHD\obranking\\test_chi.csv", 'w') as csvfilew1:
>    fields = ['index', 'feature name', 'p_value']
>    csvwriter1 = csv.DictWriter(csvfilew1, fieldnames=fields)
>    csvwriter1.writeheader()
>    for i in range(1, 5502):
>        csv_data = dict().fromkeys(fields)
>        csv_data['index'] = i
>        cols =[]
>        cols.append(int(0))
>        cols.append(int(i))
>
>        df = pd.read_csv("D:\PHD\obranking\\demo.csv", usecols=cols)
>        df.apply(LabelEncoder().fit_transform)
>        X = df.drop(labels='label', axis=1)
>        Y = df['label']
>        chi_scores = chi2(X, Y)
>        if(chi_scores[1] < 0.05):
>            f_name = str(X.columns)
>            f_name = f_name[8:-19]
>            csv_data['feature name'] = f_name
>            p_val = str(chi_scores[1])
>            p_val = p_val[1:-1]
>            csv_data['p_value'] = p_val
>            print(csv_data)
>            csvwriter1.writerow(csv_data)
>            #print(csv_data)
>            #print(f_name + p_val)
>            #print(str(X.col + str(chi_scores[1]))
>test_chi.csv is created but it remains empty after execution of the code. although when i am printing csv_data it gets printed but not written in csv using writerow(csv_data). Also there are no field names in the csv even writeheader() seems to not work. I am confused what is wrong. Could someone help????
>-- 
>https://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list