Python - 用于在CSV中跨行写入的循环

时间:2022-01-11 00:03:39

Basically, I have some code that asks a user to input a comma-separated list of currencies and 2 dates. I want to take these currencies, and write it to a CSV file in the following format:

基本上,我有一些代码要求用户输入以逗号分隔的货币列表和2个日期。我想采用这些货币,并按以下格式将其写入CSV文件:

Date, Curr1, Curr2, Curr3, ... , Currn
---, x, x, x, ..., n
---, x, x, x, ..., n

Where x is the exchange rate for the currency on the given date. I have tried using csv.writer like this:

其中x是给定日期货币的汇率。我试过像这样使用csv.writer:

    writer = ex.csv.writer(f, delimiter=',')
    writer.writerow(['Date', codes]) 
    for i in range(delta.days + 1):
        date = real_date + ex.timedelta(days=i)
        rates = exchrates(date) #function fetches all rates on given date, returns dict
        for j in codes:
            writer.writerow([date, rates[j.upper()]])

but it doesn't write to the file how I would like, rather like this:

但它不会写入我想要的文件,而是像这样:

Date,['RSD', 'GBP']
2008-04-11,51.586749
2008-04-11,0.506908
2008-04-12,51.586749
2008-04-12,0.506674

putting each currency's value on a new row, whereas I want it like:

将每种货币的价值放在一个新行上,而我希望它像:

Date,RSD,GBP
2008-04-11,51.586749,0.506908
2008-04-12,51.586749,0.506674

I am a beginner to Python so apologies if this is trivial. Any help would be greatly appreciated.

我是Python的初学者,如果这是微不足道的话,请道歉。任何帮助将不胜感激。

Thanks.

3 个解决方案

#1


Well. The output is just, what you actually programmed it to be.

好。输出就是,你实际编程的是什么。

With writer.writerow() you should write only a list of data. Try this:

使用writer.writerow(),您应该只写一个数据列表。试试这个:

writer = ex.csv.writer(f, delimiter=',')
writer.writerow(['Date'] + codes) # codes seems to be a list, so just append it
for i in range(delta.days + 1):
    date = real_date + ex.timedelta(days=i)
    rates = exchrates(date)
    writer.writerow([date] + [rates[j.upper()] for j in codes]) # only one line

#2


You're writing the exchange rate for each currency on a new line because in the inner for cycle you're iterating through the list ot codes and you're performing a write operation for each code. Instead of

您正在为新行编写每种货币的汇率,因为在内部循环中,您将遍历列表或代码,并且您正在为每个代码执行写入操作。代替

for j in codes:
    writer.writerow([date, rates[j.upper()]])

you should do something like this:

你应该做这样的事情:

new_row = [date]
for j in codes:
    new_row.append(rates[j.upper()])
writer.writerow(new_row)

#3


writerow writes just that: an entire row. You need to build the row using codes and make one call.

作家写道:整整一行。您需要使用代码构建行并进行一次调用。

for i in range(delta.days + 1):
    date = real_date + ex.timedelta(days=i)
    rates = exchrates(date) #function fetches all rates on given date, returns dict
    writer.writerow([date] + [rates[j.upper()] for j in codes])

#1


Well. The output is just, what you actually programmed it to be.

好。输出就是,你实际编程的是什么。

With writer.writerow() you should write only a list of data. Try this:

使用writer.writerow(),您应该只写一个数据列表。试试这个:

writer = ex.csv.writer(f, delimiter=',')
writer.writerow(['Date'] + codes) # codes seems to be a list, so just append it
for i in range(delta.days + 1):
    date = real_date + ex.timedelta(days=i)
    rates = exchrates(date)
    writer.writerow([date] + [rates[j.upper()] for j in codes]) # only one line

#2


You're writing the exchange rate for each currency on a new line because in the inner for cycle you're iterating through the list ot codes and you're performing a write operation for each code. Instead of

您正在为新行编写每种货币的汇率,因为在内部循环中,您将遍历列表或代码,并且您正在为每个代码执行写入操作。代替

for j in codes:
    writer.writerow([date, rates[j.upper()]])

you should do something like this:

你应该做这样的事情:

new_row = [date]
for j in codes:
    new_row.append(rates[j.upper()])
writer.writerow(new_row)

#3


writerow writes just that: an entire row. You need to build the row using codes and make one call.

作家写道:整整一行。您需要使用代码构建行并进行一次调用。

for i in range(delta.days + 1):
    date = real_date + ex.timedelta(days=i)
    rates = exchrates(date) #function fetches all rates on given date, returns dict
    writer.writerow([date] + [rates[j.upper()] for j in codes])