简体   繁体   English

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

[英]How to read every row of CSV and invoke the value by a user function, and then save the result in an csv file?

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]

The result is like:结果是这样的:

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

How can I invoke these lists in the function below: Screen_names如何在下面的函数中调用这些列表: Screen_names

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

Here's a sample function that seems to do what you asked.这是一个示例函数,它似乎可以满足您的要求。 It works as follows:它的工作原理如下:

  1. Open your current file (fileName)打开您当前的文件(文件名)
  2. Store said file's content into a list (values[])将所述文件的内容存储到列表中 (values[])
  3. Create the file you want to write in (newValues.csv)创建要写入的文件 (newValues.csv)
  4. Create an object to write with (valueWriter)创建要写入的对象 (valueWriter)
  5. write each value from your list into the new file (the writerow() function)将列表中的每个值写入新文件(writerow() 函数)

. .

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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