Python 3.x 如何将新行从一个CSV附加到另一个现有CSV
在下面的代码中,程序读取已存在的CSV。将显示第二个CSV文件,代码需要在输出CSV中提取并附加新行。代码工作正常。但是,我想知道是否有可能编写一段更干净/更好的代码 如何更好地清理代码Python 3.x 如何将新行从一个CSV附加到另一个现有CSV,python-3.x,Python 3.x,在下面的代码中,程序读取已存在的CSV。将显示第二个CSV文件,代码需要在输出CSV中提取并附加新行。代码工作正常。但是,我想知道是否有可能编写一段更干净/更好的代码 如何更好地清理代码 input_file = 'Cached_data//cache_2.csv' output_file = 'Output//current-date_output.csv' with open(output_file, 'r') as out_file: lines_out = [] for
input_file = 'Cached_data//cache_2.csv'
output_file = 'Output//current-date_output.csv'
with open(output_file, 'r') as out_file:
lines_out = []
for line in out_file:
lines_out.append(line)
with open(input_file, 'r') as in_file, open(output_file, 'a+') as out_file:
for line in in_file:
if line in lines_out:
continue
else:
out_file.write(line)
内容缓存_2.csv:
"line 5"
"line 6"
"line 7"
"line 8"
output.csv的内容:
"line 1"
"line 2"
"line 3"
"line 4"
试试这个。它会起作用的
input_file = 'Cached_data//cache_2.csv'
output_file = 'Output//current-date_output.csv'
with open(input_file, 'r') as in_file, open(output_file, 'a+') as out_file:
lines_out = []
for line in out_file:
lines_out.append(line)
for line in in_file:
if line not in lines_out:
out_file.write(line)
+模式允许您读取和编辑文件。因此不需要第一个循环来读取文件
进一步减少:-
input_file = 'Cached_data//cache_2.csv'
output_file = 'Output//current-date_output.csv'
with open(input_file, 'r') as in_file, open(output_file, 'a+') as out_file:
lines_out = []
[lines_out.append(line) for line in out_file]
[out_file.write(line) for line in in_file if line not in lines_out]
试试这个。它会起作用的
input_file = 'Cached_data//cache_2.csv'
output_file = 'Output//current-date_output.csv'
with open(input_file, 'r') as in_file, open(output_file, 'a+') as out_file:
lines_out = []
for line in out_file:
lines_out.append(line)
for line in in_file:
if line not in lines_out:
out_file.write(line)
+模式允许您读取和编辑文件。因此不需要第一个循环来读取文件
进一步减少:-
input_file = 'Cached_data//cache_2.csv'
output_file = 'Output//current-date_output.csv'
with open(input_file, 'r') as in_file, open(output_file, 'a+') as out_file:
lines_out = []
[lines_out.append(line) for line in out_file]
[out_file.write(line) for line in in_file if line not in lines_out]
以下是一个简单的解决方案:
with open(output_file, "r+") as out_file, open(input_file, "r") as inp_file:
out_content = out_file.readlines()
out_file.writelines([line for line in inp_file.readlines() if line not in out_content])
以下是一个简单的解决方案:
with open(output_file, "r+") as out_file, open(input_file, "r") as inp_file:
out_content = out_file.readlines()
out_file.writelines([line for line in inp_file.readlines() if line not in out_content])
将文件2.csv添加到文件1.csv的末尾。。如果第一次也找不到,它将创建file1.csv
将文件2.csv添加到文件1.csv的末尾。。如果第一次没有找到,它将创建file1.csv。它应该是+而不是r+,因为它将覆盖输出文件而不是附加内容。因此,它只会写入两个文件之间不常见的内容,但会删除所有内容,然后再写入。@VaibhavJadhav它不会覆盖内容,因为它先读取它,然后读取它,但在执行写入操作时,它会删除旧的输出文件,并在两个文件之间写入具有不常见数据的新输出文件文件.r+打开以进行读写。流位于文件的开头。a+打开,用于在文件末尾读取和追加写入。如果文件不存在,则创建该文件。读取文件的初始位置在文件的开头,但输出会附加到文件的结尾,但在某些Unix系统中,无论当前搜索位置如何。@VaibhavJadhav使用r+,该位置最初位于开头,但读取一次会将其推向结尾,允许您附加。对于a+,位置最初位于末尾。它应该是a+,而不是r+,因为它将覆盖输出文件,而不是附加内容。因此,它只会写入两个文件之间不常见的内容,但会删除所有内容,然后再写入。@VaibhavJadhav它不会覆盖内容,因为它先读取它,然后读取它,但在执行写入操作时,它会删除旧的输出文件,并在两个文件之间写入具有不常见数据的新输出文件文件.r+打开以进行读写。流位于文件的开头。a+打开,用于在文件末尾读取和追加写入。如果文件不存在,则创建该文件。读取文件的初始位置在文件的开头,但输出会附加到文件的结尾,但在某些Unix系统中,无论当前搜索位置如何。@VaibhavJadhav使用r+,该位置最初位于开头,但读取一次会将其推向结尾,允许您附加。使用+时,位置最初位于末尾。