Getting “newline inside string” while reading the csv file in Python?

后端 未结 3 1589
时光说笑
时光说笑 2021-01-02 13:14

I have this utils.py file in Django Architecture:

def range_data(ip):
    r = []
    f = open(os.path.join(settings.PROJECT_ROOT, \'static\', \'csv \', 
             


        
3条回答
  •  旧巷少年郎
    2021-01-02 13:59

    1. You can preprocess the csv by removing the newline like below.

      import csv
      
      content = open("GeoIPCountryWhois.csv", "r").read().replace('\r\n','\n')
      
      with open("GeoIPCountryWhois2.csv", "w") as g:
          g.write(content)
      

      Then Use GeoIPCountryWhois2 for csv reader.

    2. A wild Guess using a lineterminator may solve your problem

      for num,row in enumerate(csv.reader(f,lineterminator='\n'))
      

      See also: http://docs.python.org/lib/csv-fmt-params.html

提交回复
热议问题