如何读取CSV的每一行并通过用户函数调用该值,然后将结果保存在csv文件中?

时间:2015-10-30 03:19:33

标签: python

import csv
import time
with open("AuthorName.csv", "rb") as f:
    reader = csv.reader(f, delimiter="\t")
    for line in enumerate(reader):
        time.sleep(2)
        print line[1]

结果如下:

['rhaf_hq']
['Digixp']
['bujhoapp']
['styhuang']
['mkasanm']

如何在以下函数中调用这些列表: Screen_names

print json.dumps(get_user_profile(twitter_api, screen_names=["SocialWebMining"]), indent = 1 )

1 个答案:

答案 0 :(得分:0)

这是一个示例函数,似乎可以满足您的要求。 它的工作原理如下:

  1. 打开当前文件(fileName)
  2. 将所述文件的内容存储到列表中(values [])
  3. 创建您要写入的文件(newValues.csv)
  4. 使用(valueWriter)
  5. 创建要写入的对象
  6. 将列表中的每个值写入新文件(writerow()函数)
  7. import csv
    
    def make(fileName):
        with open(fileName) as file:
            values = []
            #Putting all your file's data in a list
            for each in file:
                each = each.strip()
                values.append(each)
        #Creating your new file
        with open('newValues.csv','w') as newFile:
            #creating a variable to write with
            valueWriter = csv.writer(newFile)
            #writing each individual value to your new file
            for each in values:
                valueWriter.writerow([each])