我正在执行一个程序,我将excel文件导出到.txt,我必须将此.txt文件导入到我的程序中。主要目标是从每一行中提取相同的部分,但问题是在.txt文件中,excel的行被制作成一个没有/ n的大字符串。你知道在程序中是否有办法将它们分开,如果有的话我该怎么办呢?
我正在使用的文件可以在http://we.tl/YtixI1ck6l下载 到目前为止,我正在尝试像
这样的东西urxvt
但是因为它没有按行排列,只是在一行中我没有得到任何好的结果。
答案 0 :(得分:0)
以该文件为例,使用csv module进行解析。
示例:
<?php
$id = (isset($_GET['id']) ? $_GET['id'] : NULL);
if ($userid) {
$userinfo = $db->query_first("SELECT * FROM `users` WHERE `id` = '$userid'");
print_r($userinfo);
}
?>
打印:
<!DOCTYPE html>
<html>
<head>
<!--START HEAD-->
<?php include 'head.php'; ?>
<link rel='stylesheet' type='text/css' href="/profile.css"/>
<title><?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>'s profile</title>
<!--END HEAD-->
</head>
<body>
<!--START BODY-->
<?php
// At the top of the page we check to see whether the user is logged in or not
if(empty($_SESSION['user']))
{
include'navigationbar.php';
} else {
include'navigationbar2.php';
}
?>
<!--MAIN CONTENT-->
<div class="movedown"></div>
<div class="content">
<?php
$userid = (isset($_GET['id']) ? $_GET['id'] : NULL);
if ($userid) {
$userinfo = $db->query_first("SELECT * FROM `users` WHERE `id` = '$userid'");
print_r($userinfo);
}
?>
<!--MAIN PROFILE-->
<div class="username">
<?php if(empty(htmlentities($_SESSION['user']['clan'], ENT_QUOTES, 'UTF-8'))){
echo "";
}else{
echo "[".htmlentities($_SESSION['user']['clan'], ENT_QUOTES, 'UTF-8')."]";} ?>
<?php echo htmlentities($_SESSION['user']['username'], ENT_QUOTES, 'UTF-8'); ?>
<div class="fr">ID: <?php echo htmlentities($_SESSION['user']['id'], ENT_QUOTES, 'UTF-8'); ?></div></div>
<div class="spacing"></div>
<div class="fl">
<!--####################################### NEED CODE FOR DEFAULT AVATAR AND CUSTOM AVATAR ######################################################-->
<!--AVATAR-->
<div class="avatarback">
<div class="avatar"><img src="/bin/avatar/default_picture/default.jpg" width="180px"/></div>
</div>
<!--####################################### NEED CODE FOR DEFAULT AVATAR AND CUSTOM AVATAR ######################################################-->
<!--TITLES / SIGNS-->
<div class="signs"><br/>
<div class="title"><div class="fl">Title: </div>
<div class="fr"><?php echo htmlentities($_SESSION['user']['title'], ENT_QUOTES, 'UTF-8'); ?> </div></div><!--TITLE-->
<div class="spacing"></div>
<div class="warnings"><div class="fl">Warnings: </div>
<div class="fr"><?php
if($_SESSION['user']['banned'] == 1){
echo "<div class='red'>Banned</div>";
}else{
echo htmlentities($_SESSION['user']['warnings'], ENT_QUOTES, 'UTF-8');
}
?> </div></div><!--WARNINGS-->
<div class="spacing"></div>
<div class="joined"><div class="fl">Joined: </div>
<div class="fr"><?php echo htmlentities($_SESSION['user']['timestamp'], ENT_QUOTES, 'UTF-8'); ?> </div></div><!--JOINED-->
</div>
</div>
<div class="padding"></div>
<!--BIO-->
<div class="bioback">
<?php if(empty(htmlentities($_SESSION['user']['bio'], ENT_QUOTES, 'UTF-8'))){
echo "This user has not set any bio yet...";
}else{
echo htmlentities($_SESSION['user']['bio'], ENT_QUOTES, 'UTF-8'); } ?>
</div>
<!--SETTINGS TAB-->
<div class="etc-back">
<div><a href="/messages">Message</a></div>
<div class="spacing2"></div>
<div><a href="/settings">Settings</a></div>
</div>
<br/>
<br/>
<div class="padding"></div>
<div class="email">
<div class="fl">E-mail:</div><div class="fr"><a href="mailto:<?php echo htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8'); ?>"><?php echo htmlentities($_SESSION['user']['email'], ENT_QUOTES, 'UTF-8'); ?></a></div>
</div>
</div>
</body>
</html>
(顺便说一句,你可能遇到的这个特定文件的问题是行终止只是来自Mac Classic OS的CR。你可以在打开文件时使用Universal Newline模式在Python中修复它...)
答案 1 :(得分:-1)
Excel正在导出带有回车符(\ r)而不是换行符(\ n)的文本文件。
ppi = []
with open("Model_Oralome.txt",'r') as f:
lines = f.readlines()
lines = lines[0].split('\r')
从这里你可以遍历每一行。因为看起来你想要第一列的值:
lines = lines[1:]
for line in lines:
content = line.split('\t')
ppi.append(content[0])