1
  #FREQ File Create

name = fixed_data['Sample ID'].unique()
number = fixed_data.shape[0]
temp_list = pd.DataFrame(
    {'ids': name,
     'nums': number,
    })

fileAppendName1 = concon + "FREQ.FREQ"
with open(fileAppendName1, 'a') as f:
    temp_list.to_csv(f, header=False, index = False)  

This code compiled by Jupyter notebook create FREQ file property and that file looks like this:

name[i], num[i]
name[i], num[i]
name[i], num[i]
name[i], num[i]

But if I download the file as .py and try to run it from cmd by doing python file.py when i open the property file in wordpad I got this:

name[i], num[i]name[i], num[i]name[i], num[i]name[i], num[i]

all in one line.

in MS notepad I got:

name[i], num[i]

name[i], num[i]

name[i], num[i]

with an empty line between.

Can someone explain to me why? And can someone give me advice about what I should change in the code to reach first (from Jupyter compilator) result?

EDIT: I forgot. That sample of code work in a big loop working on many files.

1 Answer 1

5

Replace

with open(fileAppendName1, 'a') as f:
    temp_list.to_csv(f, header=False, index = False)  

with

temp_list.to_csv(fileAppendName1, mode=‘a’, header=False, index = False)  
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I forgot to write than my sample of code work on many files, and I need appending DF. So ur method works fine but I had to add "mode='a'". Thank U very much for the help. Can u explain to me why my solution didn't work properly?
From the documentation: If a file object is passed it should be opened with newline=’‘, disabling universal newlines.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.