简体   繁体   中英

Removing extra formatting from a python list element ( [''] )

Re-learning python after not using it for a few years - so go nice on me. The basis, is I am reading in data from a.csv file, the information i am reading in is as follows

E1435
E46371
E1696
E27454

However, when using print(list[0]) for example, it produces

['E1435']

I am trying to use these pieces of data to interpolate into an API request string, and the " [' '] " in them is breaking the requests - basically, I need the elements in the list to not have the square brackets and quotes when using them as variables.

My interpolation is as follows, in case the way I'm interpolating is the problem:

req = requests.get('Linkgoeshere/%s' % list[i])

Edit; A sample of the data i'm using is listed above with "E1435, E46371" etc. each item in the csv is a new row in the same column. As per a request, i have produced a minimal reproduction of my experience.

import csv
#list to store data from csv
geoCode = []
#Read in locations from a designated file
with open('Locations.csv','rt')as f:
  data = csv.reader(f)
  for row in data:
    geoCode.append(row)
i=0
for item in geoCode:
  #print the items in the list
  print(geoCode[i])
  i+=1

It appears that list[i] is itself a nested list, so you need another subscript to get to the element inside it:

print(list[i][0])

NB: Avoid naming variables list as it overrides the built-in list type. Try using a plural word like codes or ids instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM