用逗号分割TextFile中的每一行,并将每一分割插入列表(2)

时间:2019-03-26 23:25:26

标签: python

im是python的新手,im正在寻找将2个数组的文本文件输出拆分并将其添加到有序数组的方法

我的文本文件看起来像这样

635992586,3718353429462250608
512101346,-5360506753539257325
464545614,2956310376478608644
448112867,-8362724325646859899
610706243,-3741068853182057127
583963913,-8936195924352638585
718532836,1783727609775441510
629842082,7499962062290955876
474659002,6480312120027362804
674787762,-3939702333232947477
457685296,-728255804367320714
445202854,-308676821356105829
762185989,-450762068712661096
345639374,6008304336530590920
561355579,-9018640421044127624
642304902,-3543147906925834368
493117557,2869749777644350319
393747681,812127138086916717
659310463,8747373126642898620
645009089,2643962417775803568
457146237,-5936132366977615159
725713836,-1906036700187282129
573896405,1703425138937300221

我的目标是创建2个数组,并通过此方法将其拆分 arr 1采用列表右侧的长字段(作为字符串)

和arr2将整数作为找到列表左侧的字符串

examp =

arr1[0] = 5360506753539257325
arr2[0] = 512101346

并保留到文本文件结尾

string myArray [100]; int array_count = 0;

ifstream file((path+dicfile).c_str());
std::string line;

while (std::getline(file, line))
{
    std::istringstream iss(line);
    std::string str;
    while (std::getline(iss, str, ','))
    {
        myArray[array_count] = str; // value to array
        cout << str << "\n";
        strings.push_back(str);
        array_count++;
    }
}

1 个答案:

答案 0 :(得分:1)

您可以使用python标配的csv模块以csv文件的形式打开和读取文本文件:

import csv

list1 = []
list2 = []
with open('file.txt', 'r') as file:
    my_reader = csv.reader(file, delimiter=',')
    for row in my_reader:
        list1.append(row[0])
        list2.append(row[1])

print(list1)
print(list2)

输出:

列表1

  

['635992586','512101346','464545614','448112867','610706243','583963913','718532836','629842082','474659002','674787762','457685296','445202854 ','762185989','345639374','561355579','642304902','493117557','393747681','659310463','645009089','457146237','725713836','573896405']

列表2

  

['3718353429462250608','-5360506753539257325','2956310376478608644','-8362724325646859899','-3741068853182057127','-8936195924352638585','1783727609775445441510','7499962062290955876','648031212002736280280',' -728255804367320714','-308676821356105829','-450762068712661096','60083043365305909909','-9018640421044127624','-3543147906925834368','2869749777644350319','812127138086916717','8747373126642898620','36639624159775803 -1906036700187282129','1703425138937300221']

我认为这可以满足您的要求,但这是在python中(就像您标记的一样),但是您的示例代码在python中不是 ...