Python CSV模块意外行为

时间:2014-03-19 17:05:37

标签: python csv

我已经使用以下格式将变量写入文本文件:

val1 val2 val3 val4 val5

我想使用以下代码分隔这些变量,其中'data2'是要写的字符串:

csv.register_dialect('sas', delimiter=',', quoting=csv.QUOTE_NONE)
            with open(filepath, "w") as f:
                writer = csv.writer(f, dialect='sas')
                writer.writerows(data2)

上述值是通过提交以下格式的url语句生成的:

www.finance.yahoo.com&s=var1+var2+var3+var4+var5&f=metric

然而,我得到的输出是这样的:

v
a
l
1

v
a
l
2

v
a
l
3

等...

我尝试过使用'.QUOTE_ALL',但只是重复上面的内容,但是在引号中。我希望最终输出为:

var1, var2, var3, var4

请注意,varx的实​​际值可以是从多个单词字符串到带有','和'。'的数字数据的任何值。中的字符,所以我不能将它们用作分隔符,也不能用空格。

我使用csv模块代码的相关代码如下:

import concurrent.futures
import urllib.request
import json
import csv


myurls2 = {}                     #define dictionary
for x in range(1, 15):
    for y in range(1, 87):

        strvar1 = "%s" % (x)
        strvar2 = "%s" % (y)

#define dictionary values from text file (URL strings)

        with open("C:\\Python33\\NASDAQ Stock Strings\\NASDAQ_Config_File_{}_{}.txt".format(x,y),"r") as f:
            myurls2[x,y] = f.read().replace('\n', '')            
            #print("myurls_" + str(strvar1) + "_" + str(strvar2) + "=", myurls2[x,y])
            #print(myurls2[x,y])

#use parallel processing to submit there url strings

URLS = myurls2.values()

# Retrieve a single page and report the url and contents
def load_url(key, url, timeout):
    conn = urllib.request.urlopen(url, timeout=timeout)
    return conn.readall()


# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
    # Start the load operations and mark each future with its URL
    future_to_url = {executor.submit(load_url, key, url, 60): (key, url)
                 for key, url in myurls2.items()}
    c = 0

    #create file names from dictionary keys and do some formatting

    for future in concurrent.futures.as_completed(future_to_url):
        key, url = future_to_url[future]    
        a = url.rsplit("=", 1)[-1]
        uuu = key
        uu = str(uuu)
        uv = uu[1:-1]
        uva,uvb = uv.split(",")
        uvbb = uvb.lstrip()
        u = uva + "_" + uvbb + "_"
        u = u.rstrip()
        print("key =", u)
        c += 1


        #if statement to define a goes here

        #some more formatting for the filename

        print("a = ", a)
        print('b =', b)
        print('c =', c)
        filename = "%s" % (b)
        filename = filename.lstrip()
        filename2 = u
        filepath = "C:\\Python33\\Stock Data\\" + str(filename2) + "NASDAQ_" + str(filename) + ".txt"

        #pass content returned from url submission to text file via CSV module

        data = future.result()

        try:
            csv.register_dialect('sas', delimiter=',', quoting=csv.QUOTE_ALL)
            with open(filepath, "w") as f:
                #writer = csv.writer(f, dialect='sas')
                #writer.writerow(data)
            do json processing here

有谁可以看到这里的问题是什么?

1 个答案:

答案 0 :(得分:0)

使用writer.writerow() 单数(不是writer.writerows(),复数形式),并传入值列表

writer.writerow([var1, var2, var3, var4])

不要传递一个字符串; csv模块处理;每一行都是列的列表或元组。

您将单个字符串传递给writer.writerows()方法。它需要一个行列表;它本质上是这样的:

def writerows(self, rows):
    for row in rows:
        self.writerow(row)

循环一个字符串会为您提供单个字符,因此如果data2是一个字符串,则上面将这些字符逐个传递给self.writerow()writer.writerow()方法需要一系列列;一个字符的字符串仍然是一个序列,但只包含一个元素,即单个字符。因此,写入文件的每一行都包含单个字符。

csv模块完全能够处理带有逗号的列;它会为你正确引用这些值。