A solution to prevent overwriting when the Python file is looping to write lines

  • 2021-01-25 07:45:18
  • OfStack

Existing problems:

Using written code


 with open(r'F:\PythonFiles\PycharmFile\ssq.csv', 'w', encoding='utf-8-sig', newline='') as csvFile:
   csv.writer(csvFile).writerow([col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11]) #  to csv Insert in file 1 line 

When a row is looped into an CSV file, the subsequent data always overwrites the previous data

Solution:

Change the write attribute "w" to the attribute "a" to indicate a circular write


 with open(r'F:\PythonFiles\PycharmFile\ssq.csv', 'a', encoding='utf-8-sig', newline='') as csvFile:
   csv.writer(csvFile).writerow([col1,col2,col3,col4,col5,col6,col7,col8,col9,col10,col11]) #  to csv Insert in file 1 line 


Related articles: