相关疑难解决方法(0)

如何逐行读取文件到列表中?

如何在Python中读取文件的每一行并将每一行存储为列表中的元素?

我想逐行读取文件,并将每行附加到列表的末尾.

python string file readlines

2028
推荐指数
22
解决办法
342万
查看次数

使用.readlines()时摆脱\n

我有一个包含值的.txt文件.

值如下所示:

Value1
Value2
Value3
Value4
Run Code Online (Sandbox Code Playgroud)

我的目标是将值放在列表中.当我这样做时,列表看起来像这样:

['Value1\n', 'Value2\n', ...]

\n是没有必要的.

这是我的代码:

t = open('filename.txt', 'r+w')
contents = t.readline()

alist = []

for i in contents:
    alist.append(i)
Run Code Online (Sandbox Code Playgroud)

python readline python-2.7

209
推荐指数
8
解决办法
28万
查看次数

Python导入csv列表

我有一个包含大约2000条记录的CSV文件.

每条记录都有一个字符串和一个类别.

This is the first line, Line1
This is the second line, Line2
This is the third line, Line3
Run Code Online (Sandbox Code Playgroud)

我需要将此文件读入一个看起来像这样的列表;

List = [('This is the first line', 'Line1'),
        ('This is the second line', 'Line2'),
        ('This is the third line', 'Line3')]
Run Code Online (Sandbox Code Playgroud)

如何将此导入csv到我需要使用Python的列表中?

python csv

167
推荐指数
7
解决办法
42万
查看次数

如何在Python中将逗号分隔的字符串转换为列表?

给定一个字符串,该字符串是由commma分隔的多个值的序列:

mStr = 'A,B,C,D,E' 
Run Code Online (Sandbox Code Playgroud)

如何将字符串转换为列表?

mList = ['A', 'B', 'C', 'D', 'E']
Run Code Online (Sandbox Code Playgroud)

python parsing tuples list

142
推荐指数
5
解决办法
23万
查看次数

如何从文件读取Python数组?

我想从txt文件编写和读取Python数组。我知道如何编写它,但是读取数组需要我提供有关数组长度的参数。我没有提前数组的长度,有没有一种方法可以读取数组而不计算其长度。我的工作代码如下。如果我在a.fromfile中提供2作为第二个参数,它将读取数组的第一个元素,我希望读取所有元素(基本上是要重新创建的数组)。

from __future__ import division
from array import array

L = [1,2,3,4]
indexes = array('I', L)

with open('indexes.txt', 'w') as fileW:
    indexes.tofile(fileW)

a = array('I', [])
with open('indexes.txt', 'r') as fileR:
    a.fromfile(fileR,1)
Run Code Online (Sandbox Code Playgroud)

python arrays

3
推荐指数
1
解决办法
2263
查看次数

How to convert list to 20 column array

I am making a program that takes a text file that looks something like this:

1
0
1
1
1

and converts it into a list:

['1','0','1','1','1']

The file has 400 lines so I want to convert it into an array that's 20 columns by 20 rows.

python arrays list

-1
推荐指数
1
解决办法
58
查看次数

标签 统计

python ×6

arrays ×2

list ×2

csv ×1

file ×1

parsing ×1

python-2.7 ×1

readline ×1

readlines ×1

string ×1

tuples ×1