python 读取csv文件转成字符串,在Python中将CSV文件的行读取为字符串

这篇博客介绍了如何在Python中读取CSV文件中的数字ID,将每行ID转换为字符串,通过API请求获取名称,并最终将这些名称存储回CSV文件。博主作为编程新手,成功地将CSV文件中的数字转换为列表,并通过API获取到相应的名称,但还在学习如何将结果写回到新的CSV文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

So I have a string of id's in numbers saved as CSVs

And I want is to convert each rows on this csv file to be a string/list in python

mylist=[411211, 1234404, 5711427, 13600442, 13600641, 13601660, 13619537, 15302899 ...]

Then each numbers on this string will go through an API request and then spit out Names

Then I want to be able to store these names in csv again.

I know the code for converting numbers to names via API works because I tried to manually type in the numbers as lists in python and was able to print out names in python. But I'm having a trouble working with csv...

Okayyy so converting into list somehow worked...

I still don't fully understand how... (forgot to mention I'm a novice to this whole programming thing...)

import urllib2

import json

import csv

with open('jfl42facebooknumberid.csv', 'rU') as csvfile:

reader = csv.reader(csvfile, quoting=csv.QUOTE_NONE)

for row in reader:

myid='. '.join(row)

try:

myfile=urllib2.urlopen("http://graph.facebook.com/"+ myid +"?fields=name")

myjson=json.loads(myfile.read())

print myjson["name"]

except:

print myid

But this prints me the results I want! I just need to figure out how to store into a csv...

解决方案

import csv

with open('eggs.csv', 'rb') as csvfile:

spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')

for row in spamreader:

print ', '.join(row)

I think it is a great and simple example.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值