4

I am trying to load a series of CSV files as vectors in PyQGIS. I run this code to try to get the first one:

uri = 'file://D:/Saved Stuff/gulags/1.40.csv?delimiter=;&yField=y&xField=x'
layer = QgsVectorLayer(uri, '1.40', 'delimitedtext')
layer.isValid()
False

Obviously it won't load when I run QgsMapLayerRegistry.instance().addMapLayer(layer). When I add the CSV manually to a map it loads fine. Here is what that looks like

name;y;x;total

AMURSKII ZhELEZNODOROZhNYI ITL;51.375889;128.134147;84053

BELOMORO-BALTIISKII ITL;62.912774;34.456850;78918

BIRSKII ITL;48.999752;132.461997;11634

Can anyone help me with this?

Taras
35.8k5 gold badges77 silver badges152 bronze badges
asked Oct 14, 2015 at 16:53
2
  • What happens when you do this? uri='file://D:/Saved Stuff/gulags/1.40.csv?delimiter=%s&xField=%s&yField=%s' % (";","x","y"); Commented Oct 14, 2015 at 17:06
  • Still gives me false. The modulo is just a standin Commented Oct 14, 2015 at 17:22

1 Answer 1

10

The problem was with your URI. (note the third slash)

Use the following as a working example:

uri = 'file:///D:/Saved Stuff/gulags/1.40.csv?delimiter=;&yField=y&xField=x'
layer = QgsVectorLayer(uri, '1.40', 'delimitedtext')
Taras
35.8k5 gold badges77 silver badges152 bronze badges
answered Oct 14, 2015 at 21:37

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.