[英]Python loading txt file and split lines by position in line
我是新來的,是 python 初學者。 我收到了一個包含 100k 行的文本文件,每行包含 120 個字符。 每行代表 14 列的數據,但由於某些值較短,而另一些值則用空白填充。 這樣我就沒有像“,”這樣的分隔符。 如果我選擇空白作為分隔符,則值不會 go 到正確的列。
線條就像
O2020august Opel .
L2015may BMW .
L2016april Mercedes.
O2021january Opel .
L2023februaryAudi .
我被困住了
df = pd.read_csv('text.txt', index_col=0, header = None)
print (data)
我對建議的任何方法感到高興。 不需要是 pandas。
干杯珍妮
或者您可以使用一個簡單的助手 function 為您完成這項工作。
def split_by_pos(string_to_split, *args):
"""
Splits a string at the given positions
:param string_to_split: the string to be split
:param args: the positions where the function will split the string.
:return: the splitted string as a tuple
"""
return_value = list()
args = sorted(args)
previous = 0
for position in args:
return_value.append(string_to_split[previous:position])
previous = position
return_value.append(string_to_split[previous:])
return tuple(return_value)
with open("a_random_file.txt", "r", encoding="utf-8") as fp:
lines = fp.readlines()
for line in lines:
print(split_by_pos(line, 1, 5, 12))
我相信這樣的事情可以解決你的問題。
for line in txt:
#line should point something like that => "O2020august Opel"
print(line)
s1 = line[:1]
s2 = line[1:5]
s3 = line[5:13]
.
.
.
print(s1, s2, s3)
您可以使用 Python 文件的readline
和readlines
方法讀取 API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.