Python Forum

Full Version: CSV writer - no output??
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am unable to use Pandas on a Python 2.7 environment.
I am appending all output into a new file using the csv library.
I am having to use Visual Code, which appears a bit fuzzy.
Yesterday, I didn't get any errors with the same code???

Now I am getting a writer error:
writer.writerow(row)
File "C:\python38\lib\csv.py", line 154, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "C:\python38\lib\csv.py", line 147, in _dict_to_list
wrong_fields = rowdict.keys() - self.fieldnames
AttributeError: 'list' object has no attribute 'keys'

I would appreciate any pointers.
Thanks,
Clive

##### START    
for filename in glob.glob(Path+'\*.jpg'):
    #print(filename)
    with open(latlongfile, 'w' ) as csvfile:
    #### Get Filename, lat/Long
        #### Returns: Latitude, Longitude and Altitude
        geo_tag = gpsphoto.getGPSData(filename)
        #reader = csv.DictReader(csvfile)     
        print(geo_tag)
        latitude = geo_tag.get('Latitude')
        longitude = geo_tag.get('Longitude')
        #for row in reader:
        print(row['filename'], row['latitude'], row['longitude'])
        #print(filename, latitude, longitude)
        writer = csv.DictWriter(csvfile, headersCSV)
        # Pass the data in the dictionary as an argument into the writerow() function
        writer.writerow(row)
        # Close the file object
        csvfile.close()
#### END
Python 2.7 has not been supported since January 1, 2020 when it reached end of life.

you can try: pip2 install pandas <rev #> from command line

you will need a compatible version of pandas, see old document here: https://pandas.pydata.org/pandas-docs/ve...stall.html
(Mar-28-2022, 06:43 PM)Larz60+ Wrote: [ -> ]Python 2.7 has not been supported since January 1, 2020 when it reached end of life.

you can try: pip2 install pandas <rev #> from command line

you will need a compatible version of pandas, see old document here: https://pandas.pydata.org/pandas-docs/ve...stall.html

Thanks, I have tried 15 Pandas that are 86_64, none would install.
So have to change the Python to use csv not pandas.
Yesterday Visual Code was totally unresponsive.. No error, no output..
Today it is spewing out data, but each record to a new line.

##### START    
for filename in glob.glob(Path+'\*.jpg'):
    #print(filename)
    with open(latlongfile, 'a', encoding='utf-8', newline ='' ) as csvfile:
    #### Get Filename, lat/Long
        #### Returns: Latitude, Longitude and Altitude
        geo_tag = gpsphoto.getGPSData(filename)   
        #print(geo_tag)
        latitude = geo_tag.get('Latitude')
        longitude = geo_tag.get('Longitude')
        row = [filename], [latitude], [longitude]
        lkewriter = csv.writer(csvfile, headersCSV)
        #lkewriter.writerow(fieldnames)
        lkewriter.writerows(row)
        # Close the file object
    csvfile.close()
#### END   
Rather than getting: data on a single line
C:\opt\data\images\layers\image.jpg, 38.10264166666666,-84.483105
I am getting data on three lines??

C:\opt\data\image.jpg
38.10264166666666
-84.483105
Please answer (I didn't notice the filename before.):

  1. Is this truly a .jpg file?
  2. If so, you cannot read the data using 'a' (append) mode as you are trying to do, nor is it a csv format (which is text).
  3. or is it a text file containing lat/long data?
  4. what are your imports
  5. Please attach a sample file.
The script reads the metadata from the .jpg, this is shown below
I have to use 'a', If I use 'w' it overwrites and I only get one row, not 100 rows.

The issue is each row is exported to three rows, NOT one row.
I should get:
C:\opt\data\image.jpg, 38.10264166666666, -84.483105

The csv writes out:
C:\opt\data\image.jpg
38.10264166666666
-84.483105

Thanks
Clive
The issue seems to be because I used: lkewriter.writerows(row)

If I use lkewriter.writerow(row)
It writes a single row