Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSV writer - no output??
#1
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
Larz60+ write Mar-28-2022, 06:36 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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
Reply
#3
(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.
Reply
#4
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
Reply
#5
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.
Reply
#6
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
Reply
#7
The issue seems to be because I used: lkewriter.writerows(row)

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Csv writer meaning of quoting mg24 2 1,106 Oct-01-2022, 02:16 PM
Last Post: Gribouillis
  [split] NameError: name 'csvwriter' is not defined. Did you mean: 'writer'? cathy12 4 3,201 Sep-01-2022, 07:41 PM
Last Post: deanhystad
Sad pandas writer create "corrupted" file freko75 1 2,736 Jun-14-2022, 09:57 PM
Last Post: snippsat
  Closing Files - CSV Reader/Writer lummers 2 2,564 May-28-2020, 06:36 AM
Last Post: Knight18

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020