Problem of writing long list of lists file to csv

subhabangalore at gmail.com subhabangalore at gmail.com
Tue May 22 06:11:38 EDT 2018


I have a list of lists (177 lists). 

I am trying to write them as file.

I used the following code to write it in a .csv file.

import  csv
def word2vec_preprocessing():
    a1=open("/python27/EngText1.txt","r")
    list1=[]
    for line in a1:
        line1=line.lower().replace(".","").split()
        #print line1
        list1.append(line1)
    lst1=list1
    lst2=lst1[:4]
    with open("my_csv.csv","wb") as f:
        writer = csv.writer(f)
        writer.writerows(lst2)

Here it is writing only the first four lists. 

I have searched for help and it seems it is an issue and 
without much of fix. 
Please see the following link. 
https://stackoverflow.com/questions/30711899/python-how-to-write-list-of-lists-to-file

I have now tried pandas and json as follows, but same result. 
my_df = pd.DataFrame(lst2)
    my_df.to_csv('sbb_csv.csv', index=False, header=False)

with open('sbb1.json', 'w') as F:
        # Use the json dumps method to write the list to disk  
        F.write(json.dumps(lst2))
    with open('sbb1.json', 'r') as F:
        B = json.loads(F.read())

    print B


I am using Python 2.7.15 (v2.7.15:ca079a3ea3, Apr 30 2018, 16:22:17) [MSC v.1500 32 bit (Intel)] on win32
in MS-Windows.

Please suggest what  error I may be doing? 

Thanking in advance.



    



More information about the Python-list mailing list