简体   繁体   English

如何编写新的csv文件作为另一个的扩展名?

[英]How to write new csv file as extension of another?

I want to write a new value to a csv file "hello.csv" to each row. 我想将新值写入csv文件“ hello.csv”的每一行。 It's the adjusted time zone for each row. 这是每行的调整后时区。


 with open('hello.csv', 'r', encoding="latin-1") as csvfile:
          readCSV = csv.reader(csvfile, delimiter=',')
          list1 = list(readCSV)
          for j in list1:
              dtstr=j[1] #string
              hours, minutes = [int(t) for t in tstr.split(':')]
              dt = datetime.strptime(dtstr, '%Y-%m-%d %H:%M:%S')         +timedelta(hours=hours+4, minutes=minutes)
              with open('helloout.csv', 'w', encoding="latin-1") as outfile:
                    outCSV = csv.writer(outfile,delimiter=',')
                    newline = j + [dt]
                    outCSV.writerow(newline)

When I use this code I become a csv file "helloout.csv" with only one row. 当我使用此代码时,我变成只有一行的csv文件“ helloout.csv”。 It's the last row of "hello.csv" with the new time in a new column. 这是“ hello.csv”的最后一行,新时间位于新列中。 I think it's overwriting the rows but I have no idea why? 我认为它正在覆盖行,但我不知道为什么?

Access modes govern the type of operations possible in the opened file. 访问模式控制着打开的文件中可能的操作类型。 It refers to how the file will be used once its opened. 它指的是文件打开后将如何使用。 In order to append a new line your existing file, you need to open the file in append mode , by setting "a" or "ab" as the mode. 为了在现有文件中添加新行,您需要以“添加”模式打开文件,方法是将“ a”或“ ab”设置为模式。

When you open with "a" mode , the write position will always be at the end of the file (an append). 当您以“ a”模式打开时,写入位置将始终位于文件末尾(附加)。 There are other permutations of the mode argument for updating (+), truncating (w) and binary (b) mode but starting with just "a" is your best. 模式参数还有其他排列方式,用于更新(+),截断(w)和二进制(b)模式,但最好以“ a”开头。 If you want to seek through the file to find the place where you should insert the line, use 'r+'. 如果要在文件中查找要插入行的位置,请使用“ r +”。

The following code append a text in the existing file: 以下代码在现有文件中附加一个文本:

with open("index.txt", "a") as myfile:
    myfile.write("text appended")

You can also use file access_mode "a+" for Open for reading and writing. 您也可以将文件access_mode“ a +”用于Open进行读写。 The file is created if it does not exist. 如果文件不存在,则创建该文件。 The stream is positioned at the end of the file . 流位于文件的末尾。 The initial file position for reading is at the beginning of the file, but output is appended to the end of the file. 读取的初始文件位置在文件的开头,但输出将附加到文件的结尾。

with open("index.txt", "a+") as myfile:
    myfile.write("New text appended")

How to append new data onto a new line? 如何将新数据追加到新行?

You can use "\\n" while writing data to file. 您可以在将数据写入文件时使用“ \\ n”。

with open("index.txt", "a") as myfile:
    myfile.write("First Line\n")
    myfile.write("Second Line\n")

Your code: 您的代码:

with open('hello.csv', 'r', encoding="latin-1") as csvfile:
          readCSV = csv.reader(csvfile, delimiter=',')
          list1 = list(readCSV)
          for j in list1:
              dtstr=j[1] #string
              hours, minutes = [int(t) for t in tstr.split(':')]
              dt = datetime.strptime(dtstr, '%Y-%m-%d %H:%M:%S')         +timedelta(hours=hours+4, minutes=minutes)
              with open('helloout.csv', 'a', encoding="latin-1") as outfile:
                    outCSV = csv.writer(outfile,delimiter=',')
                    newline = j + [dt]
                    outCSV.writerow(newline)

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

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