Python 读取带有[]和'';

Python 读取带有[]和'';,python,list,csv,Python,List,Csv,我正在尝试制作一个简单的程序,生成一个URL列表,然后从中获取。我怀疑我这样做是最有效的方式,但这就是我所拥有的 有没有办法在我的列表中去掉括号和单引号 #reads in list of towns that I want to scrape f = open('townList.csv', 'r') reader = csv.reader(f) towns= [] for row in reader: towns.append(row) #base url. the rest

我正在尝试制作一个简单的程序,生成一个URL列表,然后从中获取。我怀疑我这样做是最有效的方式,但这就是我所拥有的

有没有办法在我的列表中去掉括号和单引号

#reads in list of towns that I want to scrape 
f = open('townList.csv', 'r')
reader = csv.reader(f)
towns= []

for row in reader:
    towns.append(row)

#base url. the rest of the url follows the convention 'town-state-abbreviation'
base_url = "https://datausa.io/profile/geo/"

url_list = []

for n in range(len(towns)):
    url = f'{base_url}{towns[n]}{"-pa"}'
    url_list.append(url)

print(url_list)

with open('urls.csv', 'w') as output:
    writer = csv.writer(output, lineterminator='\n')
    for val in url_list:
        writer.writerow([val]) 
我得到的是:

https://datausa.io/profile/geo/['Easton']-pa
我想得到:

https://datausa.io/profile/geo/Easton-pa

cvs阅读器是列表的迭代器。快速解决方法可能是:

for row in reader:
    towns.append(row[0])
但这暗示您的文件不是csv文件,而是一个简单的顺序文件,每行具有on值。所以,伊姆霍,你想要的是:

#reads in list of towns that I want to scrape 
with open('townList.csv', 'r') as f:
    towns = [line.strip() for line in f]

您正在阅读的文件中有什么?